2014-02-24 119 views
1

我已閱讀了圍繞此問題的多個問題和答案,但是我無法讓他們中的任何人爲我工作。通知恢復應用程序,而不是重新啓動

我有一個通知,點擊時我想將應用程序放在前面並繼續而不是關閉並重新啓動。

這是我的通知代碼

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("example") 
      .setContentText("example"); 
    // Creates an explicit intent for an Activity in your app 
    Intent resultIntent = new Intent(this, MainActivity.class); 
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    // The stack builder object will contain an artificial back stack for the 
    // started Activity. 
    // This ensures that navigating backward from the Activity leads out of 
    // your application to the Home screen. 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
    // Adds the back stack for the Intent (but not the Intent itself) 
    stackBuilder.addParentStack(MainActivity.class); 
    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
       0, 
       PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    int mId = 0; 
    // mId allows you to update the notification later on. 
    mNotificationManager.notify(mId, mBuilder.build()); 

而在我的清單文件我有

 android:launchMode="singleTop" 

有人能看到什麼錯誤?我沒有收到任何錯誤,但通知拒絕恢復應用程序,而是重新啓動它。

+0

的http:// stackoverflow.com/questions/2232238/how-to-bring-an-activity-to-foregro未堆棧頂部 – nekavally

+0

** resultIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); **仍然導致應用程序重新啓動。 – rossd

+0

也許,你試圖帶到前面的活動已經被摧毀了? – nekavally

回答

5

通過使用這個固定的:

NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle("Sound Asleep") 
      .setContentText("Click to play and stop sounds"); 
    // Creates an explicit intent for an Activity in your app 
    final Intent notificationIntent = new Intent(this, MainActivity.class); 
    notificationIntent.setAction(Intent.ACTION_MAIN); 
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    // The stack builder object will contain an artificial back stack for the 
    // started Activity. 
    // This ensures that navigating backward from the Activity leads out of 
    // your application to the Home screen. 

    // Adds the back stack for the Intent (but not the Intent itself) 
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT); 

    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    int mId = 0; 
    // mId allows you to update the notification later on. 
    mNotificationManager.notify(mId, mBuilder.build()); 
} 

在下面的代碼清單

android:launchMode="singleTop" 
+0

這工作對我來說,但改變mId = 1,並在清單中的android:launchMode =「singleInstance」。謝謝 –

0

我有一些類似的問題,最近改變你的意圖的標誌與此標誌:

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

編輯:從您的清單中刪除該launchmode。

希望它能幫助

+0

嘗試和應用程序仍在重新加載,我很難理解爲什麼。 – rossd

+0

而不是使用stackbuilder剛剛創建的的PendingIntent resultPendingIntent = PendingIntent.getActivities(背景下,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT) 因爲你對你的resultIntent設置的標誌我不認爲它真的有必要 – murielK

+0

刪除那些具有 - \t \t的PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT); \t \t仍在重新啓動應用程序。看來沒有什麼會解決我的問題。 – rossd

0

使用,可能會有助於

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.ic_launcher) 
     .setContentTitle("Sound Asleep") 
     .setContentText("Click to play and stop sounds"); 
// Creates an explicit intent for an Activity in your app 
final Intent notificationIntent = new Intent(this, MainActivity.class); 
notificationIntent.setAction(Intent.ACTION_MAIN); 
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
// The stack builder object will contain an artificial back stack for the 
// started Activity. 
// This ensures that navigating backward from the Activity leads out of 
// your application to the Home screen. 

// Adds the back stack for the Intent (but not the Intent itself) 
PendingIntent resultPendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

mBuilder.setContentIntent(resultPendingIntent); 
NotificationManager mNotificationManager = 
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
int mId = 0; 
// mId allows you to update the notification later on. 
mNotificationManager.notify(mId, mBuilder.build()); 
相關問題