理想的方式来设置Android的全球未捕获的异常处理程序 [英] Ideal way to set global uncaught exception Handler in Android

查看:201
本文介绍了理想的方式来设置Android的全球未捕获的异常处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想成立一​​个全球性的未捕获的异常处理程序在我的Andr​​oid应用程序中的所有线程。所以,在我的应用程序子我订的 Thread.UncaughtExceptionHandler 作为默认处理未捕获异常的实现。

I want to set a global uncaught exception handler for all the threads in my Android application. So, in my Application subclass I set an implementation of Thread.UncaughtExceptionHandler as default handler for uncaught exceptions.

Thread.setDefaultUncaughtExceptionHandler(
                new DefaultExceptionHandler(this));

在我的实现,我想显示 AlertDialog 显示相应的异常信息。

In my implementation, I am trying to display an AlertDialog displaying appropriate exception message.

不过,这似乎并没有工作。无论何时,一个异常被抛出肚里任何线程未经处理的,我得到的股票,操作系统默认对话框(对不起! - 应用 - 已-停意外对话框)。

However, this doesn't seem to work. Whenever, an exception is thrown for any thread which goes un-handled, I get the stock, OS-default dialog ("Sorry!-Application-has-stopped-unexpectedly dialog").

什么是正确的和理想的方法来设置一个默认的处理程序捕获的异常?

What is the correct and ideal way to set a default handler for uncaught exceptions?

推荐答案

这应该是所有你需要做的。 (请确保您导致进程暂停之后 - 事情可能是在一个不确定的状态)

That should be all you need to do. (Make sure you cause the process to halt afterward -- things could be in an uncertain state.)

要检查的第一件事是,是否在Android的处理器是获取调用。这可能是你的版本被调用,但没有致命的system_server呈现出通用对话框时,看到的进程崩溃。

The first thing to check is whether the Android handler is still getting called. It's possible that your version is being called but failing fatally and the system_server is showing a generic dialog when it sees the process crash.

添加一些日志信息的处理程序的顶部,看它是否到达那里。打印从getDefaultUncaughtExceptionHandler的结果,然后抛出一个未捕获的异常会导致崩溃。保持眼睛上的logcat输出,看看发生了什么事情。

Add some log messages at the top of your handler to see if it's getting there. Print the result from getDefaultUncaughtExceptionHandler and then throw an uncaught exception to cause a crash. Keep an eye on the logcat output to see what's going on.

这篇关于理想的方式来设置Android的全球未捕获的异常处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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