2013-05-18 99 views
0

我想單擊通知在我的應用程序中啓動我的一項活動。 我設計了一個待定的意圖,如鏈接Notifications中所討論的。 但是,當我點擊通知時,會啓動一項活動,但這不是應該從我的應用程序啓動的活動。只有與我的活動具有相同類名的活動纔會啓動。我在我的活動中設置了一個斷點,並且斷點從未被擊中。 有什麼不對? NotificationListActivity是我喜歡發佈的一個。現在標題爲NotificationListActivity的活動啓動,但不是我的活動。單擊通知啓動應用程序的活動

Intent resultIntent = new Intent(thisclasscontext, NotificationListActivity.class); 
resultIntent.putExtra("MOBILENUMBER", tel); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(thisclasscontext); 
// Adds the back stack for the Intent (but not the Intent itself) 
stackBuilder.addParentStack(NotificationListActivity.class); 
// Adds the Intent that starts the Activity to the top of the stack 
stackBuilder.addNextIntent(resultIntent); 
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 
mBuilder.setContentIntent(resultPendingIntent); 
+0

參考?你的MainActiivty已經啓動,並說你有一個按鈕,當你點擊啓動出現,當你點擊通知你需要導航到NotificationListActivity?我很困惑 – Raghunandan

+0

你可以發佈你的logcat嗎?你爲什麼會有兩個同名的活動? – Raghunandan

+0

@Raghunandan當我從指定的號碼中獲得指定信息的短信時,我的應用程序發出通知。當用戶單擊我想要在我的應用程序 – Bryanyan

回答

0

您可以使用下面的,因爲它的工作原理

你想被點擊通知時推出NotificationListActivity
public void notification() 
    { 
     int mId=1; 
     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("My notification") 
       .setContentText("Hello World!"); 

     // Creates an explicit intent for an Activity in your app 
     Intent resultIntent = new Intent(MainActivity.this, SecondActivity.class); 

     // The stack builder object will contain an artificial back stack for the 
     // started Activity. 
     // This ensures that navigating backward from the Activity leads out of 
     // your application to the Home screen. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(MainActivity.this); 
     // Adds the back stack for the Intent (but not the Intent itself) 
     stackBuilder.addParentStack(SecondActivity.class); 
     // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
       stackBuilder.getPendingIntent(
        0, 
        PendingIntent.FLAG_UPDATE_CURRENT 
       ); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     // mId allows you to update the notification later on. 
     mNotificationManager.notify(mId, mBuilder.build()); 
    } 
+0

中啓動其中一個活動的通知時,此代碼在BroadcastReceiver類中實現。因此不能使用MainActivity.this – Bryanyan

+0

您可以將活動上下文傳遞給BroadCastReceiver類的構造函數 – Raghunandan

相關問題