2013-10-09 77 views
1

我閱讀了用於創建通知的文檔。因爲他們已經使用TaskStackBuilderPendingIntent for TaskStackBuilder

  1. 爲活動創建單獨的任務。
  2. 添加活動的使用addParentStack父()
  3. 添加一個意圖
  4. 最終創建的PendingIntent。

之後他們沒有使用StackBuilder對象設置在NotificationCompat.Builder對象。他們使用了PendingIntent對象。

上述所有信息(創建一個單獨的任務,以確定父活動,以確定意圖)駐留在PendingInent?

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.notification_icon) 
     .setContentTitle("My notification") 
     .setContentText("Hello World!"); 
// Creates an explicit intent for an Activity in your app 
Intent resultIntent = new Intent(this, ResultActivity.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(this); 
// Adds the back stack for the Intent (but not the Intent itself) 
stackBuilder.addParentStack(ResultActivity.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

當你在你的應用程序中使用通知會發生什麼。你首先指定一個明確的意圖(正常意圖)。然後創建TaskStackBuilder對象來訪問你wan't時通知被點擊然後用TaskStackBuildergetPendingIntent(初始化的PendingIntent參考),並把它傳遞給通知對象開始活動。

現在什麼的PendingIntent做的就是使用stackBuilder.getPendingIntent(INT ID,旗)TaskStackBuilder對象的意圖,並通過調用notification.setContentIntent(的PendingIntent對象)傳遞待定對象的通知對象

要恢復在代碼中的錯誤遵循這樣的: -

  1. 首先,你應該創建顯式意圖
  2. 然後創建TaskStackBuilder對象然後添加parentStacknextIntent
  3. 創建PendingIntent object then then get it's intent using stackBuilder.getPendingIntent(int id,Flag)。使用notification.setContentIntent(PendingIntent)

我相信你會與該指令一起做......

注: -通知對象不接受意向性對象它需要的PendingIntent對象