2014-02-24 83 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()); 
相关问题