2016-12-28 81 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()支持的任何標誌,以控制在實際發送時可以提供的意圖的哪些未指定部分。「