首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Notification OnClick事件

Notification OnClick事件
EN

Stack Overflow用户
提问于 2016-12-28 22:18:41
回答 1查看 3K关注 0票数 2

当我点击一个android通知时,我想把这个活动叫做NotificationReceiver

通知已经正确显示,但当我单击通知时,没有任何反应。

以下是通知的代码:

代码语言:javascript
复制
 private void notification_anzeigen2(){

    Log.d("Test2", "Test2 ");
    Intent intent = new Intent(this, NotificationReceiver.class);
    intent.putExtra("NotiClick",true);


    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, Intent.FILL_IN_ACTION);

    Notification n  = new Notification.Builder(this).setContentTitle("GestureAnyWhere Hintergrundservice")
                                                    .setContentText("Bitte klicken, um den Hintergrundservice zu beenden")
                                                    .setContentIntent(pIntent)
                                                    .setAutoCancel(true)
                                                    .setSmallIcon(R.drawable.ic_launcher)
                                                    .build();


    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(1, n);
    Log.d("Test3", "Test3 ");
}

下面是活动:

代码语言:javascript
复制
public class NotificationReceiver  extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {


    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null)
        {
            Log.d("Test4", "Test4");
        }
        else if (extras.getBoolean("NotiClick"))
        {
            Log.d("Test5", "Test5");
            stopService(new Intent(getApplicationContext(), HintergrundService.class));
        }

    }



    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);
}

非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2016-12-28 23:40:49

尝试:

代码语言:javascript
复制
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

您将一个与此处无关的常量作为标志传递,因此它的值可以与不需要的标志匹配,请参见PendingIntent doc

它说:“可以是FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT、FLAG_UPDATE_CURRENT,也可以是Intent.fillIn()支持的任何标志,以控制实际发送时可以提供哪些未指定的意图部分。”

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41363933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档