如果活动崩溃,如何清除通知?
在我的应用程序中,我正在创建一个设置了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一切都会崩溃,甚至谷歌应用程序也是如此。我使用 Thread.setUncaughtExceptionHandler() 和以下处理程序代码:
Everything crashes, even Google applications. I use
Thread.setUncaughtExceptionHandler()
and the following handler code:这或多或少与强制关闭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.
不。当你的应用程序崩溃时,崩溃后就无能为力了!要么捕获异常并处理它(尽管
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).