2013-11-25 22 views
1

所以我有一個通知在我的狀態欄,當用戶點擊時提出了一個沒有標題的活動,以複製一個對話框。但我有一個問題。如果我打開應用程序,那麼只需單擊主頁按鈕,然後單擊狀態欄中的通知即可。它將堆疊在頂部的notificato活動展示在應用程序的主要活動中。我想要做的就是這樣做,當我點擊通知時,它會清除所有底部的活動,以免永遠不會發生。這裏是我用來啓動通知活動的代碼清除舊的活動,使他們不會出現在新的活動點擊

// Send Notification 
Intent intent1 = new Intent(this, Dialog.class); 
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent1, 0); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
.setSmallIcon(R.drawable.ic_stat_quick_tweet_icon) 
.setContentTitle("Dialog") 
.setContentText("Touch for more options") 
.setWhen(System.currentTimeMillis()) 
.setContentIntent(pIntent) 
.setAutoCancel(false).setOngoing(true); 
NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 
Notification notif = builder.getNotification(); 
nm.notify(1, notif); 
+0

當你離開它時,你可以使用'finish()'一個活動,這會更容易 – chinglun

回答

1

這不會起作用。你寫的:

Intent intent1 = new Intent(this, Dialog.class); 
intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

它說的是:「如果活動Dialog在任務堆棧,清晰(完成)是棧中的最重要的是開始Dialog活動之前的所有活動已經存在,否則(如果在堆棧中沒有Dialog的實例),只需啓動Dialog活動「。所以你看到的是你的Dialog活動放在已經在任務堆棧中的其他活動之上。

如果希望此通知從任務堆棧中刪除所有活動,然後開始你的Dialog活動,我建議你做到以下幾點:

創建這樣的通知:

Intent intent1 = new Intent(this, MyRootActivity.class); 
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent1.putExtra("startDialog", true); 
在此

case,MyRootActivity必須是任務的根活動(即:清單中具有ACTION = MAIN和CATEGORY = LAUNCHER的活動)。

MyRootActivityonCreate()做到這一點:

super.onCreate(...); 
if (getIntent().hasExtra("startDialog")) { 
    // User has selected the notification, so we show the Dialog activity 
    Intent intent = new Intent(this, Dialog.class); 
    startActivity(intent); 
    finish(); // finish this activity 
    return; // Return here so we don't execute the rest of onCreate() 
} 
... here is the rest of your onCreate() method... 

希望這是顯而易見的。

+1

非常感謝。並解釋爲什麼它不適合我。 – Fernando

+0

還有一個問題,我將如何使這項工作成爲一項服務。因爲在我的服務類中它不能識別方法finish();或getIntent(); @David Wasser – Fernando

+0

對不起,不太明白這個問題。您無法在服務中顯示對話框。你想做什麼? –

相關問題