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

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

问题描述

在我的应用程序,我创建的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?

罗布W的

推荐答案

一切都崩溃了,甚至是谷歌应用程序。我用 Thread.setUncaughtExceptionHandler()和下面的处理程序code:

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天全站免登陆