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.


will be what is needed to remove the icon.

share|improve this answer

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;

public void onCreate() {

    notificationManager = (NotificationManager) 

    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);

protected void onPause() {
    if (isFinishing()) {
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