5

In my app, I'm creating a notification with the FLAG_ONGOING_EVENT flag set as such..

Notification notification = new Notification(iconId, text, System.currentTimeMillis());  
notification.flags |= Notification.FLAG_ONGOING_EVENT;

I'm cancelling the notification in onDestroy, but if my app crashes before calling onDestroy, is there any way to have my notification go away?

Rob W.

brockoli
  • 4,516
  • 7
  • 38
  • 45
  • possible duplicate of [Callback before Force Close of Android Activity?](http://stackoverflow.com/questions/3313960/callback-before-force-close-of-android-activity) – EboMike Oct 27 '10 at 00:06
  • 1
    @EboMike if you know of a way to write 100% robust code that never crashes (without effectively wrapping ur code all in a try-catch) for a rather complicated normal-usasge android app, then... plz - divulge how you can accomplish such methods of coding – David T. Apr 16 '14 at 21:21

3 Answers3

19

Everything crashes, even Google applications. I use Thread.setUncaughtExceptionHandler() and the following handler code:

package my.package;

import java.lang.Thread.UncaughtExceptionHandler;

import android.app.NotificationManager;
import android.content.Context;

public class CrashHandler implements UncaughtExceptionHandler
{
  private static final int NOTIFICATION_ID = 12345;

  private UncaughtExceptionHandler defaultUEH;
  private NotificationManager notificationManager;

  public CrashHandler(Context context)
  {
    this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  }

  public void uncaughtException(Thread t, Throwable e)
  {
    if (notificationManager != null)
    {
      try
      {
        notificationManager.cancel(NOTIFICATION_ID);
      }
      catch (Throwable ex)
      {
        ex.printStackTrace();
      }
    }
    notificationManager = null;

    defaultUEH.uncaughtException(t, e);
  }
}
Andrey Novikov
  • 5,563
  • 5
  • 30
  • 51
5

This is more or less the same question as Callback before Force Close of Android Activity?, so I'll repeat my answer here:

I would recommend not having your app crash in the first place. If there's something that COULD crash, just put a try/catch around it and handle it properly.

Or, as some sort of global try/catch, you can use Thread.setUncaughtExceptionHandler(). Finally, you could even consider Runtime.addShutdownHook, but that's most likely a bad idea.

Fix your crashes. That's the only sane solution.

Community
  • 1
  • 1
EboMike
  • 76,846
  • 14
  • 164
  • 167
  • 1
    Yah that particular crash has been fixed, I was just hoping there was some way to handle it better in the field for any future bugs that may crop up and cause my app to crash. – brockoli Oct 27 '10 at 13:23
2

No. When your app crashes, there's nothing that can be done after the crash! Either catch the exception and deal with it then (although catch Exception e {...} is an extremely bad idea), or make your app not crash (this would be a good tactic in my opinion).

fredley
  • 32,953
  • 42
  • 145
  • 236