通知OnClick事件

时间:2016-12-28 14:18:41

标签: android notifications

当我点击Android通知时,我想调用活动NotificationReceiver

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

以下是通知的代码:

 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 ");
}

这是活动:

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);
}

非常感谢。

1 个答案:

答案 0 :(得分:0)

尝试:

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()支持的任何标志,以控制实际发送时可以提供的意图的哪些未指定部分"