Android 全局异常捕获之CrashHandler

一个App上线或者投入到生产环境的时候崩溃了,还不知道是什么原因,这肯定是开发者的痛...

所以肯定要加入全局异常捕获,如果项目较大的话,可以考虑加入第三方诸如友盟的崩溃统计插件,以达到异常捕获的效果!

Crash,可以理解为崩溃、垮台,通常来讲就是App运行期间发生了不可预料的错误,

虽然在经历发布之前,测试人员进行了大量的测试,但是并不能保证App的正常运行,总会或多或少有一些BUG的。

Java的Thread中有一个UncaughtExceptionHandler接口,

该接口的作用主要是为了当Thread 因未捕获的异常而突然终止时,调用处理程序。

我们可以通过setDefaultUncaughtExceptionHandler方法,来改变异常默认处理程序。

实现代码如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.yuyh.utils;  
  2.   
  3. import android.content.Context;  
  4. import android.content.pm.PackageInfo;  
  5. import android.content.pm.PackageManager;  
  6. import android.content.pm.PackageManager.NameNotFoundException;  
  7. import android.os.Build;  
  8.   
  9. import java.io.PrintWriter;  
  10. import java.io.StringWriter;  
  11. import java.io.Writer;  
  12. import java.lang.Thread.UncaughtExceptionHandler;  
  13. import java.lang.reflect.Field;  
  14. import java.text.DateFormat;  
  15. import java.text.SimpleDateFormat;  
  16. import java.util.HashMap;  
  17. import java.util.Map;  
  18.   
  19. /** 
  20.  * UncaughtException处理类,当程序发生Uncaught异常的时候,有该类来接管程序,并记录发送错误报告. 
  21.  * <p> 
  22.  * Created by yuyuhang on 15/12/7. 
  23.  */  
  24. public class CrashHandler implements UncaughtExceptionHandler {  
  25.   
  26.     //系统默认的UncaughtException处理类  
  27.     private Thread.UncaughtExceptionHandler mDefaultHandler;  
  28.     //CrashHandler实例  
  29.     private static CrashHandler INSTANCE;  
  30.     //程序的Context对象  
  31.     private Context mContext;  
  32.     //用来存储设备信息和异常信息  
  33.     private Map<String, String> infos = new HashMap<String, String>();  
  34.   
  35.     private CrashHandler() {  
  36.     }  
  37.   
  38.     /** 
  39.      * 获取CrashHandler实例 ,单例模式 
  40.      */  
  41.     public static CrashHandler getInstance() {  
  42.         if (INSTANCE == null)  
  43.             INSTANCE = new CrashHandler();  
  44.         return INSTANCE;  
  45.     }  
  46.   
  47.     /** 
  48.      * 初始化 
  49.      * 
  50.      * @param context 
  51.      */  
  52.     public void init(Context context) {  
  53.         mContext = context;  
  54.         //获取系统默认的UncaughtException处理器  
  55.         mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();  
  56.         //设置该CrashHandler为程序的默认处理器  
  57.         Thread.setDefaultUncaughtExceptionHandler(this);  
  58.     }  
  59.   
  60.     /** 
  61.      * 当UncaughtException发生时会转入该函数来处理 
  62.      */  
  63.     @Override  
  64.     public void uncaughtException(Thread thread, Throwable ex) {  
  65.         if (!handleException(ex) && mDefaultHandler != null) {  
  66.             //如果用户没有处理则让系统默认的异常处理器来处理  
  67.             mDefaultHandler.uncaughtException(thread, ex);  
  68.         } else {  
  69.             try {  
  70.                 Thread.sleep(3000);  
  71.             } catch (InterruptedException e) {  
  72.                 LogUtils.e(e.toString());  
  73.             }  
  74.             //退出程序  
  75.             android.os.Process.killProcess(android.os.Process.myPid());  
  76.             System.exit(1);  
  77.         }  
  78.     }  
  79.   
  80.     /** 
  81.      * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 
  82.      * 
  83.      * @param ex 
  84.      * @return true:如果处理了该异常信息;否则返回false. 
  85.      */  
  86.     private boolean handleException(Throwable ex) {  
  87.         if (ex == null) {  
  88.             return false;  
  89.         }  
  90.         //收集设备参数信息  
  91.         collectDeviceInfo(mContext);  
  92.         //保存日志文件  
  93.         saveCrashInfo2File(ex);  
  94.         return true;  
  95.     }  
  96.   
  97.     /** 
  98.      * 收集设备参数信息 
  99.      * 
  100.      * @param ctx 
  101.      */  
  102.     public void collectDeviceInfo(Context ctx) {  
  103.         try {  
  104.             PackageManager pm = ctx.getPackageManager();  
  105.             PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(), PackageManager.GET_ACTIVITIES);  
  106.             if (pi != null) {  
  107.                 String versionName = pi.versionName == null ? "null" : pi.versionName;  
  108.                 String versionCode = pi.versionCode + "";  
  109.                 infos.put("versionName", versionName);  
  110.                 infos.put("versionCode", versionCode);  
  111.             }  
  112.         } catch (NameNotFoundException e) {  
  113.             LogUtils.e("CrashHandleran.NameNotFoundException---> error occured when collect package info", e);  
  114.         }  
  115.         Field[] fields = Build.class.getDeclaredFields();  
  116.         for (Field field : fields) {  
  117.             try {  
  118.                 field.setAccessible(true);  
  119.                 infos.put(field.getName(), field.get(null).toString());  
  120.             } catch (Exception e) {  
  121.                 LogUtils.e("CrashHandler.NameNotFoundException---> an error occured when collect crash info", e);  
  122.             }  
  123.         }  
  124.     }  
  125.   
  126.     /** 
  127.      * 保存错误信息到文件中 
  128.      * 
  129.      * @param ex 
  130.      * @return 返回文件名称, 便于将文件传送到服务器 
  131.      */  
  132.     private String saveCrashInfo2File(Throwable ex) {  
  133.   
  134.         StringBuffer sb = new StringBuffer();  
  135.         sb.append("---------------------sta--------------------------");  
  136.         for (Map.Entry<String, String> entry : infos.entrySet()) {  
  137.             String key = entry.getKey();  
  138.             String value = entry.getValue();  
  139.             sb.append(key + "=" + value + "\n");  
  140.         }  
  141.   
  142.         Writer writer = new StringWriter();  
  143.         PrintWriter printWriter = new PrintWriter(writer);  
  144.         ex.printStackTrace(printWriter);  
  145.         Throwable cause = ex.getCause();  
  146.         while (cause != null) {  
  147.             cause.printStackTrace(printWriter);  
  148.             cause = cause.getCause();  
  149.         }  
  150.         printWriter.close();  
  151.         String result = writer.toString();  
  152.         sb.append(result);  
  153.         sb.append("--------------------end---------------------------");  
  154.         LogUtils.e(sb.toString());  
  155.         return null;  
  156.     }  
  157. }  

然后我们在Application中,对CrashHandler进行初始化。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import android.app.Application;  
  2. import android.content.Context;  
  3.   
  4. import com.yuyh.utils.CrashHandler;  
  5.   
  6. /** 
  7.  * Created by yuyuhang on 15/12/7. 
  8.  */  
  9. public class App extends Application {  
  10.   
  11.     public static Context mContext;  
  12.   
  13.     @Override  
  14.     public void onCreate() {  
  15.         super.onCreate();  
  16.         CrashHandler crashHandler = CrashHandler.getInstance();  
  17.         crashHandler.init(getApplicationContext());  
  18.         mContext = this;  
  19.     }  
  20. }  

这样的话,当程序代码中并未捕获异常,但发生了异常的时候,就会交由CrashHandler进行处理,异常信息可以保存到日志文件中。

日志文件记录请参考:Android 日志打印工具类 可显示打印所在的方法和行号

这样子,就能把异常信息及其异常发生所在的位置,保存在日志文件中。


原文地址:http://blog.csdn.net/yyh352091626/article/details/50599195

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值