在Notification
類的文件我看到這一點:Android通知:是否需要Intent.FLAG_ACTIVITY_NEW_TASK?
公衆的PendingIntent contentIntent
點擊展開的狀態項時執行的意圖。如果這是一項活動,則必須包含
FLAG_ACTIVITY_NEW_TASK
標誌,該標誌要求您負責Tasks and Back Stack文檔中所述的任務管理。特別是,請務必閱讀通知部分Handling Notifications以瞭解從通知啓動應用程序的正確方法。
我讀過上面鏈接的材料,但我仍然沒有得到它。爲什麼在點擊通知開始活動時需要標記FLAG_ACTIVITY_NEW_TASK
?我嘗試下面的代碼:
NotificationManager manager = (NotificationManager)context.
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(
android.R.drawable.stat_notify_sync, title,
System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context, NotifiedActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // IS THIS REALLY REQUIRED??
PendingIntent pt = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, title, text, pt);
manager.notify(0, notification);
我跑了上面的代碼,既沒有了intent.setFlags
線,而且似乎是沒有什麼區別。事實上,我發現的許多代碼示例根本沒有這一行。那麼爲什麼文檔說FLAG_ACTIVITY_NEW_TASK
標誌是必須的,在通知處理中它究竟有什麼不同?
非常明確的解釋。謝謝! – ycsun