PendingIntent 在活动重新加载时继续触发
我有一个 PendingIntent 单击通知时触发,该通知基本上将意图发送到我的主菜单(设置为 singleTop 启动模式)。这会触发 onNewIntent,然后加载我想要的数据并转到加载了该数据的另一个活动。不幸的是,如果应用程序当前关闭,则 onNewIntent 不起作用,因此我必须从 onCreate 调用 onNewIntent 并在 onNewIntent 上进行检查。
一切都工作得很好,除了现在在您触发通知后,pendingIntent 似乎很高兴每次调用 onCreate 时都会触发它,它不会消失。我不知道如何在使用后进行检查,甚至清除待处理的意图。我的代码如下:
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if(extras != null && extras.containsKey("notification")) {
if(extras.getBoolean("notification", false)) {
loadData(extras.getString("data"));
Intent myIntent = new Intent(this, OtherClass.class);
startActivity(myIntent);
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
//Code snippet of notification setup
notification = new Notification(R.drawable.icon, "Notification Reminder", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, MainMenu.class);
Bundle bundle = new Bundle();
contentTitle = "Notification_Title";
bundle.putBoolean("notification", true);
bundle.putString("data", contentTitle.toString());
notificationIntent.putExtras(bundle);
notificationIntent.setData((Uri.parse("application://"+SystemClock.elapsedRealtime())));
contentTitle = contentTitle + " click me!";
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, notificationText, contentIntent);
I have a PendingIntent firing on click on a notification that basically send an intent to my main menu (which is set to singleTop launch mode). This fires the onNewIntent which then loads the data I want and goes to another activity with that data loaded. Unfortunately onNewIntent does not work if the application is currently closed so I had to put a call to onNewIntent from onCreate and include a check in then onNewIntent.
Everything works splendidly except now after you fire your notification the pendingIntent seems to be happy with firing every single time onCreate is called, it doesn't go away. I am not sure how to throw in a check or even clear the pending intent after a use. My code is as follows:
protected void onNewIntent(Intent intent) {
Bundle extras = intent.getExtras();
if(extras != null && extras.containsKey("notification")) {
if(extras.getBoolean("notification", false)) {
loadData(extras.getString("data"));
Intent myIntent = new Intent(this, OtherClass.class);
startActivity(myIntent);
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
//Code snippet of notification setup
notification = new Notification(R.drawable.icon, "Notification Reminder", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationIntent = new Intent(this, MainMenu.class);
Bundle bundle = new Bundle();
contentTitle = "Notification_Title";
bundle.putBoolean("notification", true);
bundle.putString("data", contentTitle.toString());
notificationIntent.putExtras(bundle);
notificationIntent.setData((Uri.parse("application://"+SystemClock.elapsedRealtime())));
contentTitle = contentTitle + " click me!";
contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, notificationText, contentIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用 PendingIntent.FLAG_ONE_SHOT 标志即可:
Just use PendingIntent.FLAG_ONE_SHOT flag like that: