Android Refresh Activity from Notification
I have a program in which I call a notification. The notification, if you pull it down, launches a new activity.
mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.stat_sys_secure_green;
CharSequence tickerText = "Br开发者_开发问答owser Security Enabled";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Security Vulnerability Detected";
Intent notificationIntent = new Intent(this, PrivacyMessage.class);
//Test Extra
notificationIntent.putExtra("Primary Key", "Primary Text");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
The problem comes later in the code, when I want to refresh the secondary activity. The main activity should be able to dynamically change the extras in it. I tried doing this by launching a new intent.
CharSequence contentTitle = "Browser Security";
CharSequence contentText = "Test New Notification";
Intent intent = new Intent(this, PrivacyMessage.class);
notification.icon = R.drawable.stat_sys_secure_orange;
intent.putExtra("Test Thing", "Test Value");
//intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent cI = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(getApplicationContext(), "New Title", "NewText", cI);
mNotificationManager.notify(HELLO_ID, notification);
Now, when I execute that code, the new notification title pops up, the icon color changes, and the pulldown reflects the new title and addition information. However, when I click on it, it does not launch the activity with the new intent. Instead, it just pulls out the old activity with the old extras. I tried both FLAG_ACTIVITY_CLEAR_TOP, and FLAG_ACTIVITY_NEW_TASK, but neither one seems to clear the old secondary activity and create a new one. Any idea on how I might do that?
Apparently this is actually a bug/feature of the android environment. Unless a pendingIntent() is passed with a unique requestCode, it simply retrieves the old intent that was originally passed with that number.
Details can be found here: http://groups.google.com/group/android-developers/browse_thread/thread/ad855bb57042c2bd/e84c8d6fececf6e4?lnk=gst&q=notification#e84c8d6fececf6e4
The solution they came up with was to simply increment the requestCode every time pendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags)
is called, and set the flags the way I had done it originally with
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Which, doesn't seem like a perfect solution, but it works. Thank you guys for your help!
I think, first of all, you should forget about the FLAG_ACTIVITY_NEW_TASK
, cause this would open a new task (group of activities) without clearing anything you previously opened. The FLAG_ACTIVITY_CLEAR_TOP
wouldn't vbe useful for you either, cause, if I understand the scenario correctly your taks has only two activities, and your target activity is the secondary.
So here it's my question... If the second piece of code is executed within an Activity context, why don't you just call startActivity
with the new extras? This would allow you to handle the new extras on the onStart
method of the secondary activity.
Regards.
Are you overriding onNewIntent() to catch the new intent, or just calling getIntent()? The onNewIntent() documentation says that getIntent() will continue to return the original intent used to launch the activity, unless you call setIntent() from onNewIntent().
精彩评论