In my application I have several services running. When user stop application (UI part) services remain running in the background and display notifications ( each service has one) in status bar. When clicking on it dialog appears with choice to cancel appropriate service.
And here is my problem. When something goes wrong and application crashes notifications remains in the status bar area. Is it possible to clear all notifications before showing standard android force close dialog?
The real bug is NPE when I try to open activity clicking on notification. It's fixed. But I only want to know how to clear everything when application crashes.
Here is my final solution inspired by mice's post. In application in on create method I register Thread.setDefaultUncaughtExceptionHandler()
@Override
public void onCreate() {
super.onCreate();
this.notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
UncaughtExceptionHandler appHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
this.notificationManager.cancelAll();
defaultHandler.uncaughtException(thread, ex);
}
};
Thread.setDefaultUncaughtExceptionHandler(appHandler);
}
thanks
You may call
Thread.setDefaultUncaughtExceptionHandler()
with a handler to be called when some Exception is not handled by your app in the thread. This handler is invoked in case Thread dies due to an unhandled exception.
Here you'd do needed cleanup work.
This is exactly how ACRA crash reporting library is catching crashes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With