2010-10-28 59 views
11

我想編程我的通知RESUME我的應用程序,而不是簡單地啓動我的應用程序的新實例...我基本上是尋找它做同樣的事情,當Home按鈕被長時間按下和應用程序從那裏恢復。Android:如何從通知恢復應用程序?

這是我目前在做什麼:

void notifyme(String string){ 

    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager mNotificationManager = (NotificationManager) 
               getSystemService(ns); 

    int icon = R.drawable.notification_icon;  // icon from resources 
    CharSequence tickerText = string + " Program Running...";  // ticker-text 
    long when = System.currentTimeMillis();   // notification time 
    Context context = getApplicationContext();  // application Context 
    CharSequence contentTitle = *********; // expanded message title 
    CharSequence contentText = string + " Program Running...";//expanded msg text 

    Intent notificationIntent = new Intent(this, Main.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(
               this, 0, notificationIntent, 0); 

    // the next two lines initialize the Notification, using the configurations 
    // above 
    Notification notification = new Notification(icon, tickerText, when); 
    notification.setLatestEventInfo(context, contentTitle, contentText, 
                   contentIntent); 
    final int HELLO_ID = 1; 
    mNotificationManager.notify(HELLO_ID, notification); 
} 

我猜測,新的原意是問題所在...任何幫助,將不勝感激!

+0

可能重複此屬性:HTTP:// stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification,http://stackoverflow.com/questions/3356095/how-to-bring-android-existing-activity-to-front-via-通知 – Philipp 2013-02-17 04:13:08

+0

這幫了我http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather -than-making-a-new-intent/39482464#39482464 – TharakaNirmana 2016-09-14 07:51:25

回答

12

你需要設置你的標誌

notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

另外,如果你從來沒有希望這是一個重複的活動給它在清單

android:launchMode="singleTask" 
+0

這隻適用於當通過主頁按鈕退出應用程序...但通過後退按鈕退出應用程序時不起作用......任何想法如何解決這個問題??? – 2010-10-29 00:39:21

+0

在您的notificationIntent中設置動作以表示您正在恢復,在創建時檢查該動作並相應地執行相應操作,將您需要的任何數據正確地恢復到意向 – schwiz 2010-10-29 07:09:08

+1

Reg。 @FrankBozzo的評論:默認行爲是關閉/銷燬後退按鈕上的活動(這至少是adb logcat告訴我的) - 所以你不能恢復到被殺的活動......但是我相信你可以覆蓋默認的後退按鈕行爲(請參閱'WebChromeClient'事件)不要退出應用程序。 – Philzen 2013-03-15 01:23:56