How to bring application to front when the notification icon is clicked (from Service)?
I have an application that plays media with the help of a service. This runs smoothly with the help of a service ( ongoing notification is created ). When the back key is pressed the application goes to background. The problem is when I click the notification 开发者_StackOverflow社区icon a new instance of my application is created every time. How can I get my application from the background to the front with the help of the notification bar?
My notification looks like this:
private void notifyMe() {
final NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon_audio, "Online!",
System.currentTimeMillis());
note.flags = Notification.FLAG_ONGOING_EVENT;
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, PlayMedia.class), 0);
note.setLatestEventInfo(this, "Media Payer", "Online",i);
mgr.notify(NOTIFY_ME_ID, note);
}
THE SOLUTION I got it to work by adding the following line in the manifest file: android:launchMode="singleInstance" The intent flag didn't have any effect, thought that was odd...
The intent that you pass to PendingIntent.getActivity needs to have the appropriate flag set to bring a running application to the front. So:
Intent startActivity = new Intent(this,PlayMedia.class );
startActivity.setFlags(Intent.*appropriate flag*);
PendingIntent i = PendingIntent.getActivity(this, 0, startActivity, 0);
Go to http://developer.android.com/reference/android/content/Intent.html and search (ctrl+f) for "flag_". There you will find a list of the flags you can use.
Just a variation on the answer and a comment on the question. I didn't need to add android:launchMode="singleInstance" as Alpar suggests. This is what I did to make an app come to the foreground.
// this code brings the application from background to foreground
// when the Notification icon is clicked on.
// create new notification
int icon = R.drawable.ic_notify;
Notification notification = new Notification(icon, "Ticker Text", System.currentTimeMillis());
// Create new Intent pointing to activity you want it to come back to
Intent notificationIntent = new Intent(this, MainApp.class);
// Add new intent to a pending intent
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// add that pendingIntent to the new notification
notification.setLatestEventInfo(getApplicationContext(), "Title", "Text", contentIntent);
// send the intent to the NotificationManager
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(UPLOADER_ID, notification);
All this does not work anymore in Android 5. I tried all the above suggestions and my app kept getting destroyed and restarted whenever I clicked the notification.
To fix it, all I needed to do was add 2 flags to the Intent:
Intent resultIntent = new Intent(context, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
This will bring your app back to front as if it was tapped from the launcher screen.
精彩评论