2010-09-08 55 views

回答

14

致電setLatestEventInfo()Notification對象,提供一個PendingIntent,當他們點擊通知抽屜中的條目時將開始您的活動。演示這個是a sample project

+0

當點擊通知欄上的通知時,我可以開始活動。但看起來它創建新的活動,雖然目前的活動正在運行。我試圖在調用PendingIntent時將Flag傳入'Intent',但看起來沒有工作。那麼在點擊通知時如何關閉當前活動才能創建新活動? – 2015-04-03 01:59:18

+0

@HuyTower:''Intent''上的'setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)'應該可以工作。 – CommonsWare 2015-04-03 10:37:57

+0

@CommonsWare:完美適合我! – 2017-11-17 11:12:17

11

假設notif是你Notification對象:

Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0); 
notif.contentIntent = contentIntent; 
+1

我可以在點擊通知欄上的通知時開始活動。但看起來它創建新的活動,雖然目前的活動正在運行。我試圖在調用PendingIntent時將Flag傳入'Intent',但看起來沒有工作。那麼在點擊通知時如何關閉當前活動才能創建新活動? – 2015-04-03 02:00:03

1

下面是代碼調用活動通知被點擊

Notification notif = new Notification(R.drawable.ic_launcher,"List of Contacts...", System.currentTimeMillis()); 
Intent notificationIntent = new Intent(context,AllContacts.class); 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,   notificationIntent, 0); 
notif.setLatestEventInfo(context, from, message, contentIntent); 
nm.notify(1, notif); 
+1

我可以在點擊通知欄上的通知時開始活動。但看起來它創建新的活動,雖然目前的活動正在運行。我試圖在調用PendingIntent時將Flag傳入'Intent',但看起來沒有工作。那麼在點擊通知時如何關閉當前活動才能創建新活動? – 2015-04-03 02:00:08