Android使用UncaughtExceptionHandler捕获全局异常

原文出处:http://orgcent.com/android-uncaughtexceptionhandler-exception/

Android系统的“程序异常退出”,给应用的用户体验造成不良影响。为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理。通过Thread.setDefaultUncaughtExceptionHandler()方法将异常处理类设置到线程上即可。

1、异常处理类,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
public class CrashHandler implements UncaughtExceptionHandler {
    public static final String TAG = "CrashHandler" ;
    private static CrashHandler INSTANCE = new CrashHandler ( ) ;
    private Context mContext ;
    private Thread. UncaughtExceptionHandler mDefaultHandler ;

    private CrashHandler ( ) {
    }

    public static CrashHandler getInstance ( ) {
        return INSTANCE ;
    }

    public void init ( Context ctx ) {
        mContext = ctx ;
        mDefaultHandler = Thread. getDefaultUncaughtExceptionHandler ( ) ;
        Thread. setDefaultUncaughtExceptionHandler ( this ) ;
    }

    @Override
    public void uncaughtException ( Thread thread, Throwable ex ) {
        // if (!handleException(ex) && mDefaultHandler != null) {
        // mDefaultHandler.uncaughtException(thread, ex);
        // } else {
        // android.os.Process.killProcess(android.os.Process.myPid());
        // System.exit(10);
        // }
        System. out. println ( "uncaughtException" ) ;

        new Thread ( ) {
            @Override
            public void run ( ) {
                Looper. prepare ( ) ;
                new AlertDialog. Builder (mContext ). setTitle ( "提示" ). setCancelable ( false )
                        . setMessage ( "程序崩溃了..." ). setNeutralButton ( "我知道了", new OnClickListener ( ) {
                            @Override
                            public void onClick (DialogInterface dialog, int which ) {
                                System. exit ( 0 ) ;
                            }
                        } )
                        . create ( ). show ( ) ;
                Looper. loop ( ) ;
            }
        }. start ( ) ;
    }

    /**
     * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑
     *
     * @param ex
     * @return true:如果处理了该异常信息;否则返回false
     */

    private boolean handleException ( Throwable ex ) {
        if (ex == null ) {
            return true ;
        }
        // new Handler(Looper.getMainLooper()).post(new Runnable() {
        // @Override
        // public void run() {
        // new AlertDialog.Builder(mContext).setTitle("提示")
        // .setMessage("程序崩溃了...").setNeutralButton("我知道了", null)
        // .create().show();
        // }
        // });

        return true ;
    }
}

2、线程绑定异常处理类

1
2
3
4
5
6
7
8
9
10
11
12
public class CrashHandlerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate (Bundle savedInstanceState ) {
        super. onCreate (savedInstanceState ) ;
        setContentView (R. layout. main ) ;
        CrashHandler crashHandler = CrashHandler. getInstance ( ) ;  
        crashHandler. init ( this ) ;   //传入参数必须为Activity,否则AlertDialog将不显示。
        // 创建错误
        throw new NullPointerException ( ) ;
    }
}

Demo下载地址:http://code.google.com/p/android-custom-view/downloads/list

转载请注明地址: http://orgcent.com/android-uncaughtexceptionhandler-exception/ | 萝卜白菜的博客

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值