如果活动崩溃,如何清除通知?

发布于 2024-09-29 15:08:26 字数 310 浏览 0 评论 0原文

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

Notification notification = new Notification(iconId, text, System.currentTimeMillis());  
notification.flags |= Notification.FLAG_ONGOING_EVENT;

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

罗布·W.

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;

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?

Rob W.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若能看破又如何 2024-10-06 15:08:26

一切都会崩溃,甚至谷歌应用程序也是如此。我使用 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);
  }
}

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);
  }
}
玩世 2024-10-06 15:08:26

这或多或少与强制关闭Android Activity之前的回调?<相同的问题/a>,所以我将在这里重复我的答案:

我建议首先不要让您的应用程序崩溃。如果有什么东西可能会崩溃,只需在它周围放置一个 try/catch 并正确处理它。

或者,作为某种全局 try/catch,您可以使用 Thread.setUncaughtExceptionHandler()。最后,您甚至可以考虑 Runtime.addShutdownHook,但这很可能是一个坏主意。

修复你的崩溃问题。这是唯一明智的解决方案。

This is more or less the same question as Callback before Force Close of Android Activity?, so I'll repeat my answer here:

I would recommend not having your app crash in the first place. If there's something that COULD crash, just put a try/catch around it and handle it properly.

Or, as some sort of global try/catch, you can use Thread.setUncaughtExceptionHandler(). Finally, you could even consider Runtime.addShutdownHook, but that's most likely a bad idea.

Fix your crashes. That's the only sane solution.

≈。彩虹 2024-10-06 15:08:26

不。当你的应用程序崩溃时,崩溃后就无能为力了!要么捕获异常并处理它(尽管 catch Exception e {...} 是一个极其坏主意),或者让您的应用程序不崩溃(这将是我认为这是一个很好的策略)。

No. When your app crashes, there's nothing that can be done after the crash! Either catch the exception and deal with it then (although catch Exception e {...} is an extremely bad idea), or make your app not crash (this would be a good tactic in my opinion).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文