如何構(gòu)建一個(gè)PendingIntent取決于你要啟動(dòng)的activity的類型。當(dāng)從Notification中啟動(dòng)一個(gè)activity時(shí),你必須保存用戶的導(dǎo)航體驗(yàn)。在下面的代碼片段中,點(diǎn)擊Notification啟動(dòng)一個(gè)新的activity,這個(gè)activity有效地?cái)U(kuò)展了Notification的行為。在這種情形下,就沒必要人為地去創(chuàng)建一個(gè)返回棧(更多關(guān)于這方面的信息,請(qǐng)查看 Preserving Navigation when Starting an Activity)
Intent resultIntent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder;
...
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
更多建議: