Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring application to front when the notification icon is clicked (from Service)?

Tags:

android

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 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...

like image 896
Alpar Torok Avatar asked Jul 04 '11 10:07

Alpar Torok


People also ask

How do I set Notification icons?

1 Tap Notification Settings on the notification panel or tap the Settings app. 2 Tap Notifications. 3 Tap App icon badges.


2 Answers

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.

like image 141
Marmoy Avatar answered Oct 02 '22 20:10

Marmoy


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);
like image 40
JPM Avatar answered Oct 02 '22 19:10

JPM