单击通知时,我正在使用以下代码打开我的应用程序
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.cancel(Constants.PUSH_ID);
mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
问题是,如果该应用程序已经在运行,它将在我当前的活动之后打开启动应用程序。
Intent x = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
我如何关闭旧的并启动新的应用程序。
好的,我找到了一个非常酷而简单的解决方案。我只是将一个布尔值传递给我的意图,例如i.putExtra("shouldRestart", true);
在我的SplashActivity上,我检查
if(getIntent().getBooleanExtra("shouldRestart", false))
{
// we need to clear all activities from top
finishAffinity();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句