2012-07-31 102 views
0

在我的應用程序,在一個活動(AndroidNotification.java)我做了以下內容:Android的通知 - 如何把活動前沒有召回活動

1)我顯示通知用戶按Home鍵時按鈕。

2)當用戶單擊通知我顯示出與該活動的應用程序 - AndroidNotification.java

3)我關閉的通知欄上的通知時,我的應用程序來前(即在的onResume方法)

下面的代碼工作,但問題是電話通知的活動(AndroidNotification.java)再次

我的問題是我不想要重新啓動的活動,通知須攜帶

活動從後臺開始,它不應該稱爲重啓。

當前行爲的結果是再次調用活動,以使整個功能再次起作用。那就是問題所在 。

我的代碼:

private NotificationManager mNotificationManager; 
Notification notifyDetails; 
...... 
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     notifyDetails = new Notification(R.drawable.ic_launcher, 
      "Notification, Click Me!", System.currentTimeMillis()); 

    Context context = AndroidNotification.this; 
    CharSequence contentTitle = "Notification Test"; 
    CharSequence contentText = "Get back to Application on clicking me."; 

    Intent notifyIntent = new Intent(context, AndroidNotification.class); 
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 

    PendingIntent intent = PendingIntent.getActivity(
      AndroidNotification.this, 0, notifyIntent,0); 

    // Set properties to notify object - its title, text 
    notifyDetails.setLatestEventInfo(context, contentTitle,contentText, intent); 

    // Notify user in status bar 
    mNotificationManager.notify(SIMPLE_NOTFICATION_ID, notifyDetails); 

是什麼,需要在AndroidManifest.xml

添加對特定活動

請幫助我。

+0

什麼是錯的當前行爲解決了嗎? – Axarydax 2012-08-01 05:29:49

+0

在當前行爲中,活動再次被調用(重新啓動),以使整個功能再次起作用。那就是問題 – 2012-08-01 05:42:35

+0

,如果它按照你的方式工作,當Android耗盡內存並且必須殺死你的活動並釋放它的內存時會發生什麼?那麼它就沒有其他辦法可以重新開展活動了。你將如何解決這個問題? – Axarydax 2012-08-01 07:36:44

回答

2

我已經通過添加以下在創建方法

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { 
    // Activity was brought to front and not created, 
    // Thus finishing this will get us to the last viewed activity 
    finish(); 
    return; 
}