在我的应用程序中,我正在创建一个带有 FLAG_ONGOING_EVENT 标志设置的通知。
Notification notification = new Notification(iconId, text, System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
我正在取消 onDestroy 中的通知,但如果我的应用程序在调用 onDestroy 之前崩溃,有什么方法可以让我的通知消失吗?
Rob W.
一切都会崩溃,甚至谷歌应用程序也是如此。我用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);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)