1、實現UncaughtExceptionHandler,在方法uncaughtException中處理沒有捕獲的異常。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class GlobalException implements UncaughtExceptionHandler { private final static GlobalException myCrashHandler = new GlobalException(); private GlobalException() { } public static synchronized GlobalException getInstance() { return myCrashHandler; } public void uncaughtException(Thread arg0, Throwable arg1) { Trace.Log( "-------------caught Exception--" ); } } |
2、繼承Application ,在其中調用Thread方法setDefaultUncaughtExceptionHandler,來捕獲異常
代碼:
1
2
3
4
5
6
7
8
9
10
|
public class MyApplication extends Application { public void onCreate() { super .onCreate(); GlobalException handler = GlobalException.getInstance(); Thread.setDefaultUncaughtExceptionHandler(handler); } } |