NotificationCompat - 无法阻止重启
NotificationCompat - Cannot prevent restart
我想阻止系统在用户点击通知并且应用程序处于前台时重新启动我的应用程序。
我的测试:
1) adding android:launchMode="singleInstance"
2) adding intent.setAction(Intent.ACTION_MAIN)
3) Modifying flags using Intent.FLAG_ACTIVITY_SINGLE_TOP, or FLAG_ACTIVITY_REORDER_TO_FRONT, or FLAG_ACTIVITY_CLEAR_TOP
4) changing PendingIntent.FLAG_UPDATE_CURRENT to 0 (zero)
没有。每次我点击通知应用重启。但如果我在应用程序中,我想阻止它。
这是我的代码:
Intent intent = new Intent(service, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pending = PendingIntent.getActivity(
context,
aggregation,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
manager.notify(
aggregation,
new NotificationCompat.Builder(
service,
Type.isMessage(push.getEnumType())
/*mycode*/
.setCategory(category)
.setWhen(push.getWhen())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pending)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE)
.setNumber(number)
.setStyle(style)
.setAutoCancel(true)
.build());
好的,我知道了。
PendingIntent
创建后会保留在系统中,直到您重新启动系统(我不确定这种情况)或卸载您的应用程序。
您的情况的简单解决方案大约有两个步骤:
1) 正如我所说,您应该删除这行代码:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
并通过清单定义此行为:
android:launchMode="singleInstance"
2) 将 PendingIntent.FLAG_UPDATE_CURRENT
替换为 PendingIntent.FLAG_CANCEL_CURRENT
以在每次调用它的构造函数时更新 PendingIntent
。
重要事项:我建议您阅读 PendingIntent
文档以详细了解其工作原理。
我想阻止系统在用户点击通知并且应用程序处于前台时重新启动我的应用程序。
我的测试:
1) adding android:launchMode="singleInstance"
2) adding intent.setAction(Intent.ACTION_MAIN)
3) Modifying flags using Intent.FLAG_ACTIVITY_SINGLE_TOP, or FLAG_ACTIVITY_REORDER_TO_FRONT, or FLAG_ACTIVITY_CLEAR_TOP
4) changing PendingIntent.FLAG_UPDATE_CURRENT to 0 (zero)
没有。每次我点击通知应用重启。但如果我在应用程序中,我想阻止它。
这是我的代码:
Intent intent = new Intent(service, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pending = PendingIntent.getActivity(
context,
aggregation,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
manager.notify(
aggregation,
new NotificationCompat.Builder(
service,
Type.isMessage(push.getEnumType())
/*mycode*/
.setCategory(category)
.setWhen(push.getWhen())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pending)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE)
.setNumber(number)
.setStyle(style)
.setAutoCancel(true)
.build());
好的,我知道了。
PendingIntent
创建后会保留在系统中,直到您重新启动系统(我不确定这种情况)或卸载您的应用程序。
您的情况的简单解决方案大约有两个步骤:
1) 正如我所说,您应该删除这行代码:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
并通过清单定义此行为:
android:launchMode="singleInstance"
2) 将 PendingIntent.FLAG_UPDATE_CURRENT
替换为 PendingIntent.FLAG_CANCEL_CURRENT
以在每次调用它的构造函数时更新 PendingIntent
。
重要事项:我建议您阅读 PendingIntent
文档以详细了解其工作原理。