Pascal Dimassimo
Pascal Dimassimo

Reputation: 6928

Android: catching application exit

I'm currently building an app that will play sounds. When a user starts a sound, I display a status bar notification with this code:

notificationManager = (NotificationManager) activity.getSystemService(Activity.NOTIFICATION_SERVICE);

Intent intent = new Intent(activity, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);

notification = new Notification();
notification.icon = android.R.drawable.stat_notify_sync;
notification.flags |= Notification.FLAG_ONGOING_EVENT;

String title = activity.getString(R.string.notification_title);
String text = activity.getString(R.string.notification_text);

notification.setLatestEventInfo(activity, title, text, pendingIntent);

This is working fine. Later, if the user stops all sound, I use notificationManager.cancel to remove the notification, and again, this is working fine.

My problem is that I want to handle the case of someone using a task killer app. If someone uses one to kill my app, I'd like the notification icon to be removed from the status bar. I know that a lot of users like to use those kind of apps and I don't want some to complain that they can't "close" my app!

So far, I have not been able to detect when the app is about to exit. I've tried this:

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
  @Override
  public void uncaughtException(Thread thread, Throwable ex)
  {
    notificationManager.cancel(NOTIFICATION_ID);
  }
});

And also this:

Runtime.getRuntime().addShutdownHook(new Thread()
{
  @Override
  public void run()
  {
     notificationManager.cancel(NOTIFICATION_ID);              
  }
});

But this is not working. When my app gets killed, the notification icon is still there. To kill my app, I've been using an app called "Advanced Task Killer". I've also test by killing my process within DDMS.

I've noticed that the native media player is doing that. If a music is playing and you kill the music service, the icon will disappear.

So, is there a way to detect when an application is about to exit so that I can remove the notification icon? Or is there a way for the notification icon to be removed automatically if my app is killed?

Thanks!

Upvotes: 4

Views: 4542

Answers (3)

DallaRosa
DallaRosa

Reputation: 5815

There are two types of icons that appear in the Notification bar:

  1. Notification, sent by an application or the system
  2. Foreground services

You're using the first type. It's a notification sent by the application (or system) and it's goal is to deliver some information to the user. Examples: New mail arrived, Low storage space, new comment on my post. These are all informations that the user theoretically wants to check and it should stay up there until the user taps it to see the details (if there are any) or decides to remove them removing all the notifications left.

I'll start talking about foreground services with a quote from the documentation

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

For example, a music player that plays music from a service should be set to run in the foreground, because the user is explicitly aware of its operation. The notification in the status bar might indicate the current song and allow the user to launch an activity to interact with the music player.

This is the case of the Music app you mentioned in your question. The player part, inside of the Music app, is implemented as a foreground service. When the user selects a song and plays it, through the app UI, the service is started (and the icon comes up in the Notification Bar) and song reproduction starts and it can go on independently from the UI itself. When the song ends or the user stops the reproduction, the icon will disappear as the service is(should be) stopped.

To learn more about how to implement a foreground service, refer to link above (just click the word "documentation")

Upvotes: 2

Phil
Phil

Reputation: 36289

Your best bet will be to move this to a Service. In onBind(), you can then connect the Service to the main Activity, and when the app is killed, you can remove the Notification either in onUnbind or onDestroy (in the Service).

Upvotes: 2

Nicholas Jordan
Nicholas Jordan

Reputation: 656

your only hope is to do everything in onPause() and even then for anything that ^really^ needs to be saved I am doing that in a type of background thread that does not get thrown off as readily ( it's in the docs just read all of it ) and the only "workaround" i can figure for the way the whole thing runs is do saves ^while it is running^ using the Service and put check for null on non-sane values ^everywhere^ and check them in onResume() so that if you app does get dumped ( by just removing the pointer from the task-list in the os ) then at least the app returns to the user in a semi-stable state:

Dianne Hackborn, a Software Engineer who sits very near the exact center of everything Android.

Upvotes: 0

Related Questions