2016-12-28 110 views
0

我想打电话给活动NotificationReceiver,当我点击一个android通知。通知OnClick事件

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

下面是通知代码:

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

非常感谢。

+0

您是否在Manifest中注册了NotificationReceiver活动? (顺便说一下,NotificationReceiver不是一个活动的好名字,因为接收者通常意味着Android中的其他东西)。 –

回答

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