在我的应用中,我创建了一个通知,并设置了 FLAG_ONGOING_EVENT 标志......
Notification notification = new Notification(iconId, text, System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
我正在取消 onDestroy 中的通知,但是如果我的应用程序在调用 onDestroy 之前崩溃了,有什么办法可以让我的通知消失吗?
罗伯·W.
最佳答案
一切都会崩溃,甚至是 Google 应用程序。我使用 Thread.setUncaughtExceptionHandler()
和以下处理程序代码:
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);
}
}
关于android - 如果 Activity 崩溃,如何清除通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4028742/