views:

42

answers:

2

Hi, i want to put just an ICON when my application is running, also in background (home key pressed or something like that)

i mean that it's just an icon showing that my application is ON, and it have to be there allways also when my application it's in background mode

i can't find any information about how to do this

can someone give me some help please?

thanks

A: 

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 if isFinishing() 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);
    }
}
Brian
This will only show you if a given activity is running. Not if the application is still running.
Greg
@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
@Brian yep I agree ;)
Greg
A: 

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.

Greg