如果活动崩溃,如何清除通知? [英] How to clear a notification if activity crashes?

查看:21
本文介绍了如果活动崩溃,如何清除通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在创建一个带有 FLAG_ONGOING_EVENT 标志设置的通知..

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;

我正在取消 onDestroy 中的通知,但如果我的应用程序在调用 onDestroy 之前崩溃,有什么办法可以让我的通知消失?

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?

罗伯.

推荐答案

一切都崩溃了,甚至是 Google 应用程序.我使用 Thread.setUncaughtExceptionHandler() 和以下处理程序代码:

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);
  }
}

这篇关于如果活动崩溃,如何清除通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆