PendingIntent continues to fire on activity reload
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 onCrea开发者_如何学运维te 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);
Just use PendingIntent.FLAG_ONE_SHOT flag like that:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
精彩评论