程序中的异常处理(自定义UncaughtExceptionHandler)

本文介绍如何通过继承Application并重写onCreate方法来全局捕获应用程序的异常。具体步骤包括:1) 创建MyApp类继承Application,并在onCreate中设置自定义的异常处理类;2) 在AndroidManifest.xml中指定MyApp作为Application的实现;3) 实现MyCrashHandler类以处理未捕获的异常。

1.  继承Application  重写onCreate() 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
public class MyApp extends Application {
  
     //在整个应用第一次被创建出来的时候 执行 
     // 在应用程序对应的进程 第一次创建出来的时候执行 
     @Override
     public void onCreate() {
         super .onCreate();
         //把自定义的异常处理类设置 给主线程 
         MyCrashHandler myCrashHandler = MyCrashHandler.getInstance();
         myCrashHandler.init(getApplicationContext());
         Thread.currentThread().setUncaughtExceptionHandler(myCrashHandler);
     }
}

2.  修改清单文件android:label="@string/app_name" android:name="MyApp">

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
     package = "cn.itcast.crash"
     android:versionCode = "1"
     android:versionName = "1.0" >
  
     < uses-sdk android:minSdkVersion = "8" />
  
     < application
         android:icon = "@drawable/ic_launcher"
         android:label = "@string/app_name" android:name = "MyApp" >
         < activity
             android:label = "@string/app_name"
             android:name = ".CrashActivity" >
             < intent-filter >
                 < action android:name = "android.intent.action.MAIN" />
  
                 < category android:name = "android.intent.category.LAUNCHER" />
             </ intent-filter >
         </ activity >
     </ application >
  
</ manifest >

3. 实现UncaughtExceptionHandler 类

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
69
70
71
72
public class MyCrashHandler implements UncaughtExceptionHandler {
     // 保证MyCrashHandler只有一个实例
     // 2.提供一个静态的程序变量
     private static MyCrashHandler myCrashHandler;
     private Context context;
  
     // 1.私有化构造方法
     private MyCrashHandler() {
  
     }
  
     // 3.暴露出来一个静态的方法 获取myCrashHandler
  
     public static synchronized MyCrashHandler getInstance() {
         if (myCrashHandler == null ) {
             myCrashHandler = new MyCrashHandler();
         }
         return myCrashHandler;
     }
  
     public void init(Context context) {
         this .context = context;
     }
  
     // 程序发生异常的时候调用的方法
     // try catch
  
     @Override
     public void uncaughtException(Thread thread, Throwable ex) {
         System.out.println( "出现错误啦 哈哈" );
         StringBuilder sb = new StringBuilder();
         // 1.获取当前应用程序的版本号.
         PackageManager pm = context.getPackageManager();
         try {
             PackageInfo packinfo = pm.getPackageInfo(context.getPackageName(),
                     0 );
             sb.append( "程序的版本号为" + packinfo.versionName);
             sb.append( "\n" );
  
             // 2.获取手机的硬件信息.
             Field[] fields = Build. class .getDeclaredFields();
             for ( int i = 0 ; i < fields.length; i++) {
                 // 暴力反射,获取私有的字段信息
                 fields[i].setAccessible( true );
                 String name = fields[i].getName();
                 sb.append(name + " = " );
                 String value = fields[i].get( null ).toString();
                 sb.append(value);
                 sb.append( "\n" );
             }
             // 3.获取程序错误的堆栈信息 .
             StringWriter writer = new StringWriter();
             PrintWriter printWriter = new PrintWriter(writer);
             ex.printStackTrace(printWriter);
              
             String result = writer.toString();
             sb.append(result);
              
              
             System.out.println(sb.toString());
  
             // 4.把错误信息 提交到服务器
              
         } catch (Exception e) {
             e.printStackTrace();
         }
  
         // 完成自杀的操作
         android.os.Process.killProcess(android.os.Process.myPid());
     }
  
}

 

 

 

 

 

 

 





转载于:https://www.cnblogs.com/feelbest/p/3696194.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值