Android 如何防止重新启动活动的通知
我有以下代码:Android 如何防止重新启动活动的通知,android,notifications,Android,Notifications,我有以下代码: public void notify(int notificationId, int iconId, String message, Class<?> returnTo, boolean onGoing, boolean showTime, boolean autoCancel) { Intent intent = new Intent(mContext, returnTo); intent.setFlags(Intent.FLAG_ACTIVITY_
public void notify(int notificationId, int iconId, String message, Class<?> returnTo, boolean onGoing, boolean showTime, boolean autoCancel)
{
Intent intent = new Intent(mContext, returnTo);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(returnTo);
stackBuilder.addNextIntent(intent);
PendingIntent pending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationBuilder.setContentTitle("Notification")
.setContentText(message)
.setSmallIcon(iconId)
.setContentIntent(pending)
.setAutoCancel(autoCancel);
if (!showTime)
{
mNotificationBuilder.setWhen(0);
}
Notification notification = mNotificationBuilder.build();
if (onGoing)
{
notification.flags |= Notification.FLAG_ONGOING_EVENT;
}
mNotificationManager.notify(NOTIFICATION_TAG, notificationId, notification);
}
public void notify(int notificationId、int iconId、字符串消息、类returnTo、布尔进行中、布尔显示时间、布尔自动取消)
{
意向意向=新意向(mContext,returnTo);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP | intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder=TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(returnTo);
stackBuilder.addNextIntent(意向);
PendingEvent pending=stackBuilder.GetPendingEvent(0,PendingEvent.FLAG_UPDATE_CURRENT);
mNotificationBuilder.setContentTitle(“通知”)
.setContentText(消息)
.setSmallIcon(iconId)
.setContentIntent(待定)
.设置自动取消(自动取消);
如果(!showTime)
{
mNotificationBuilder.setWhen(0);
}
Notification Notification=mNotificationBuilder.build();
如果(进行中)
{
notification.flags |=notification.FLAG_持续事件;
}
mNotificationManager.notify(通知标签、通知ID、通知);
}
它可以完美地创建通知
但是,当按下通知时,调用此函数的活动(即类returnTo
参数)将再次启动,而不是返回到初始实例
为了避免这种情况,我将标志Intent.FLAG\u ACTIVITY\u CLEAR\u TOP
和Intent.FLAG\u ACTIVITY\u SINGLE\u TOP
添加到我在函数顶部创建的Intent中。遗憾的是,这不起作用,所以我尝试了其他方法
我在调用此代码的活动中添加了android:launchMode=“singleTop”
,以查看这是否有帮助。但遗憾的是,这也没有起作用
我试过在Google上查找任何东西,但只找到了标志(如前所述,我已经尝试过)和启动模式(如前所述,我也尝试过)
那么,有没有其他标志、设置等我可以试试
编辑:
android开发者网站()上的android:launchMode=“singleTop”
上似乎有一些文字
该条规定:
在其他情况下-例如,如果“singleTop”活动的现有实例在目标任务中,但不在堆栈顶部,或者如果它在堆栈顶部,但不在目标任务中-将创建一个新实例并推送到堆栈上。
使用
TaskStackBuilder
获取我的PendingEvent
是否可能有问题,关于,或者如果它位于堆栈顶部,但不在目标任务中
?可以尝试以下操作:
@SuppressLint("NewApi")
public static void showNotification(Context context, String message) {
Intent intent = new Intent(context, MainActivity.class);
// Ensures navigating back to activity, then to Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Add the activity parent chain to the stack builder.
stackBuilder.addParentStack(MainActivity.class);
// Add a new Intent to the task stack.
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Create notification.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { // API 16 onwards
Notification.Builder builder = new Notification.Builder(context)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_notifier)
.setTicker(message)
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH)
.setStyle(new Notification.BigTextStyle().bigText(message))
.setWhen(System.currentTimeMillis());
mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());
} else {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_notifier)
.setTicker(message)
.setContentText(message)
.setPriority(Notification.PRIORITY_HIGH)
.setWhen(System.currentTimeMillis());
mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());
}
}
正如我在编辑中所怀疑的那样,罪魁祸首是
TaskStackBuilder
我将代码更改为:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(returnTo);
stackBuilder.addNextIntent(intent);
致:
在此之后,通知将按照我希望的方式运行。您是否尝试过android:launchMode=“singleInstance”是的,这没有什么区别。奇怪吗?这是对
singleInstance
from:的描述,与“singleTask”相同,只是系统不会向包含实例的任务中启动任何其他活动。活动始终是其任务的唯一成员。这意味着堆栈中不能有多个同一活动的实例。如何启动通知?此函数位于帮助器类中。这个helper类从我创建实例的活动中获取上下文。从这个活动(目前是我的主要活动)中,我通过对这个方法的简单调用创建一个通知(并在该活动关闭时将其删除)。您是否使用AndroidManifest.xml
中的android:launchMode=“singleTask”
标记声明您的活动?或者,您如何定义清单中的活动?您在AndroidManifest.xml
中声明的内容可能会影响Android如何从通知启动活动。从我所看到的是,当SDK版本高于ICS时,您使用notification.Builder
而不是NotificationCompat.Builder
,对吗?正确通知.Builder
也没有帮助:(好吧,我想你可能不得不在谷歌上记录一个bug,因为我已经没有办法帮你了。对不起,我已经解决了,看看我自己的答案。)谢谢你的时间和努力。但是这会破坏导航过程导航过程呢?
PendingIntent pending = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);