我正在尝试在用户暂停我的应用时发出通知。为了使其更方便,用户可以使用通知快速返回应用程序。这是我正在使用的代码。它适用于 Android 4 之前的所有版本,但我不知道问题出在哪里。
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Titulo")
.setContentText("Titulo");
mBuilder.setOngoing(true);
// Creates an explicit intent for an Activity this
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
// put the flags
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
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);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
当我在Android 4.0及以上版本中按下通知时,活动会被重新创建而不是恢复。请帮忙解决问题,我无法使其正常工作。
编辑(忘掉清单singletop)
使用
android:launchMode="singleTop"
也没有改变结果,仍然不起作用...我的活动包含一个地图。我正在使用谷歌地图的新版本,即v2。