[关闭]
@Tyhj 2018-11-20T03:25:52.000000Z 字数 2679 阅读 1146

Android捕获Crash信息简单封装

Android


集成方法:

Step 1. Add the JitPack repository to your build file

  1. //Add it in your root build.gradle at the end of repositories:
  2. allprojects {
  3. repositories {
  4. ...
  5. maven { url 'https://jitpack.io' }
  6. }
  7. }

Step 2. Add the dependency

  1. //Add the dependency
  2. dependencies {
  3. implementation 'com.github.tyhjh:CrashUtil:v1.0.1'
  4. }

使用方法

1.在Application中初始化

  1. CrashHander.getInstance().init(this);

2.自己实现对异常进行处理

  1. CrashUtil.getInstance().setmSaveErro(new ISaveErro() {
  2. @Override
  3. public void saveErroMsg(Throwable throwable) {
  4. //异常处理
  5. }
  6. });

3.或者使用提供的异常保存的本地功能

  1. CrashUtil.getInstance().setmSaveErro(new SaveErroToSDCard(erroPath));

代码实现

Thread类中有一个getDefaultUncaughtExceptionHandler方法,可以设置系统默认异常处理器UncaughtExceptionHandler;当未捕获的异常发生的时候,系统会调用UncaughtExceptionHandleruncaughtException方法;我们需要实现一个UncaughtExceptionHandler对象,设置为系统默认异常处理器,并在它的uncaughtException中处理异常信息。

  1. public class CrashUtil implements Thread.UncaughtExceptionHandler {
  2. public static String PHONE_INFO;
  3. ISaveErro mSaveErro;
  4. Context mContext;
  5. private Thread.UncaughtExceptionHandler mDefultCrashHandler;
  6. public static CrashUtil getInstance() {
  7. return CrashHanderHolder.crashHander;
  8. }
  9. public void init(Context context) {
  10. mContext = context.getApplicationContext();
  11. PHONE_INFO = savePhoneInfo(mContext);
  12. mDefultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();
  13. Thread.setDefaultUncaughtExceptionHandler(this);
  14. }
  15. public void setmSaveErro(ISaveErro mSaveErro) {
  16. this.mSaveErro = mSaveErro;
  17. }
  18. /**
  19. * 当系统有未捕获的异常,会触发这个方法
  20. *
  21. * @param thread
  22. * @param throwable
  23. */
  24. @Override
  25. public void uncaughtException(Thread thread, Throwable throwable) {
  26. //对异常进行保存处理
  27. if (!handleException(thread, throwable) && mDefultCrashHandler != null) {
  28. mDefultCrashHandler.uncaughtException(thread, throwable);
  29. } else {
  30. //这里是应该要关闭程序的,异常会导致APP出现问题,ANR黑屏等
  31. android.os.Process.killProcess(android.os.Process.myPid());
  32. }
  33. }
  34. private boolean handleException(Thread thread, Throwable throwable) {
  35. if (thread == null || throwable == null) {
  36. return false;
  37. }
  38. if (mSaveErro != null) {
  39. mSaveErro.saveErroMsg(throwable);
  40. }
  41. return true;
  42. }
  43. static class CrashHanderHolder {
  44. static CrashUtil crashHander = new CrashUtil();
  45. }
  46. // 保存手机的信息
  47. private String savePhoneInfo(Context context) {
  48. PackageManager pm = context.getPackageManager();
  49. PackageInfo pi = null;
  50. try {
  51. pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
  52. } catch (PackageManager.NameNotFoundException e) {
  53. e.printStackTrace();
  54. }
  55. StringBuffer phoneMsg = new StringBuffer();
  56. phoneMsg.append("APP Version:")// APP的版本信息
  57. .append(pi.versionName + '_')
  58. .append(pi.versionCode + "\n")
  59. .append("OS Version:") // Android 手机版本号
  60. .append(Build.VERSION.RELEASE + '_')
  61. .append(Build.VERSION.SDK_INT + "\n")
  62. .append("Vendor:")// 手机制造商
  63. .append(Build.MANUFACTURER + "\n")
  64. .append("Model:")// 手机型号
  65. .append(Build.MODEL + "\n")
  66. .append("CUP ABI:" + Build.CPU_ABI + "\n");// CPU架构
  67. return phoneMsg.toString();
  68. }
  69. }

项目地址:https://github.com/tyhjh/CrashUtil

参考:《Android开发艺术探索》

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注