http://stackoverflow.com/questions/3973208/icon-in-the-status-bar-when-application-is-running


http://stackoverflow.com/questions/8835518/displaying-an-icon-in-status-bar-when-my-task-is-running



I want to put an icon in the status bar when ever my application is running, including when it is running in the background. How can I do this?

share|improve this question

You should be able to do this with Notification and the NotificationManager. However getting a guaranteed way to know when your application is not running is the hard part.

You can get the basic functionality of what you are desiring by doing something like:

Notification notification = new Notification(R.drawable.your_app_icon, R.string.name_of_your_app, System.currentTimeMillis());
notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
NotificationManager notifier = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notifier.notify(1, notification);

This code must be somewhere where you are sure will get fired when your application starts. Possibly in your application's custom Application Object's onCreate() method.

However after that things are tricky. The killing of the application can happen at anytime. So you can try to put something in the onTerminate() of the Application class too, but it's not guaranteed to be called.

((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(1);

will be what is needed to remove the icon.

share|improve this answer
0a96cc99c1784d6abe3cae6b8c8bf013.pngi.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giOjExNzgsImNyIjo1OTI0LCJkaSI6ImNmNTc3ODE4OTUxNDQwMDZiYjA2MzNiZjRmZWNjOGRjIiwiZG0iOjEsImZjIjo4ODEwLCJmbCI6MjQ0NCwia3ciOiJhbmRyb2lkLGljb25zLHN0YXR1c2Jhcix1aXN0YXR1c2JhciIsIm53IjoyMiwicmYiOiJodHRwczovL3d3dy5nb29nbGUuY28ua3IvIiwicnYiOjAsInByIjoxNTY4LCJzdCI6ODI3Nywiem4iOjQ0LCJ0cyI6MTM4NDA3Mjg2NjMyN30&s=Dw6vzGtHq7AdHlMV874dpeK31_E

Take a look at the Dev Guide "Creating Status Bar Notifications".

One way to achieve the goal of keeping the icon there only when the application is running is to initialize the notification in onCreate() and call cancel(int) in your onPause() method only ifisFinishing() returns true.

An example:

private static final int NOTIFICATION_EX = 1;
private NotificationManager notificationManager;

@Override
public void onCreate() {
    super.onCreate();

    notificationManager = (NotificationManager) 
        getSystemService(Context.NOTIFICATION_SERVICE);

    int icon = R.drawable.notification_icon;
    CharSequence tickerText = "Hello";
    long when = System.currentTimeMillis();

    Notification notification = new Notification(icon, tickerText, when);

    Context context = getApplicationContext();
    CharSequence contentTitle = "My notification";
    CharSequence contentText = "Hello World!";
    Intent notificationIntent = new Intent(this, MyClass.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 
        0, notificationIntent, 0);

    notification.setLatestEventInfo(context, contentTitle, 
        contentText, contentIntent);

    notificationManager.notify(NOTIFICATION_EX, notification);
}

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        notificationManager.cancel(NOTIFICATION_EX);
    }
}
share|improve this answer
 
This will only show you if a given activity is running. Not if the application is still running. –  Greg Giacovelli Oct 20 '10 at 3:37
 
@Greg: Right, this example code only correctly handles a one-activity application. It is much more difficult to determine when the system has killed the application. As you mentioned, a custom Application class is probably the best place for the code since it persists throughout the life of the application. Clearing the notification will be difficult because the system may arbitrarily decide to kill an app's process under conditions of low memory. You might try creating a service to monitor the status since the system will try to restart the service later when more memory is available. –  Brian Oct 20 '10 at 4:34
 
yep I agree ;) –  Greg Giacovelli Oct 20 '10 at 4:50