当前位置: 代码迷 >> Android >> Android捕获全局错误信息并实现上传
  详细解决方案

Android捕获全局错误信息并实现上传

热度:71   发布时间:2016-05-01 19:53:03.0
Android捕获全局异常信息并实现上传

在做项目时,经常会把错误利用异常抛出去,这样在开发时就可以通过手机抛出的异常排查错误。但是当程序开发完毕,版本稳定,需要上线时,为了避免抛出异常影响用户感受,可以用UncaughtExceptionHandler捕获全局异常,对异常做出处理。比如我们可以获取到抛出异常的时间、手机的硬件信息、错误的堆栈信息,然后将获取到的所有的信息发送到服务器中,也可以发送到指定的邮件中,以便及时修改bug。

示例:

自定义异常类实现UncaughtExceptionHandler接口,当某个页面出现异常就会调用uncaughtException这个方法,我们可以在这个方法中获取异常信息、时间等,然后将获取到的信息发送到我们指定的服务器

[java]?view plaincopy
  1. /**?
  2. ?*?自定义的?异常处理类?,?实现了?UncaughtExceptionHandler接口??
  3. [email protected]?
  4. ?*?
  5. ?*/??
  6. public?class?MyCrashHandler?implements?UncaughtExceptionHandler?{??
  7. ????//?需求是?整个应用程序?只有一个?MyCrash-Handler???
  8. ????private?static?MyCrashHandler?myCrashHandler?;??
  9. ????private?Context?context;??
  10. ????private?DoubanService?service;??
  11. ????private?SimpleDateFormat?dataFormat?=?new?SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");??
  12. ??????
  13. ????//1.私有化构造方法??
  14. ????private?MyCrashHandler(){??
  15. ??????????
  16. ????}??
  17. ??????
  18. ????public?static?synchronized?MyCrashHandler?getInstance(){??
  19. ????????if(myCrashHandler!=null){??
  20. ????????????return?myCrashHandler;??
  21. ????????}else?{??
  22. ????????????myCrashHandler??=?new?MyCrashHandler();??
  23. ????????????return?myCrashHandler;??
  24. ????????}??
  25. ????}??
  26. ????public?void?init(Context?context,DoubanService?service){??
  27. ????????this.context?=?context;??
  28. ????????this.service?=?service;??
  29. ????}??
  30. ??????
  31. ??
  32. ????public?void?uncaughtException(Thread?arg0,?Throwable?arg1)?{??
  33. ????????System.out.println("程序挂掉了?");??
  34. ????????//?1.获取当前程序的版本号.?版本的id??
  35. ????????String?versioninfo?=?getVersionInfo();??
  36. ??????????
  37. ????????//?2.获取手机的硬件信息.??
  38. ????????String?mobileInfo??=?getMobileInfo();??
  39. ??????????
  40. ????????//?3.把错误的堆栈信息?获取出来???
  41. ????????String?errorinfo?=?getErrorInfo(arg1);??
  42. ??????????
  43. ????????//?4.把所有的信息?还有信息对应的时间?提交到服务器???
  44. ????????try?{??
  45. ????????????service.createNote(new?PlainTextConstruct(dataFormat.format(new?Date())),???
  46. ????????????????????new?PlainTextConstruct(versioninfo+mobileInfo+errorinfo),?"public",?"yes");??
  47. ????????}?catch?(Exception?e)?{??
  48. ????????????e.printStackTrace();??
  49. ????????}??
  50. ??????
  51. ????????//干掉当前的程序???
  52. ????????android.os.Process.killProcess(android.os.Process.myPid());??
  53. ????}??
  54. ??
  55. ????/**?
  56. ?????*?获取错误的信息??
  57. [email protected]?
  58. [email protected]?
  59. ?????*/??
  60. ????private?String?getErrorInfo(Throwable?arg1)?{??
  61. ????????Writer?writer?=?new?StringWriter();??
  62. ????????PrintWriter?pw?=?new?PrintWriter(writer);??
  63. ????????arg1.printStackTrace(pw);??
  64. ????????pw.close();??
  65. ????????String?error=?writer.toString();??
  66. ????????return?error;??
  67. ????}??
  68. ??
  69. ????/**?
  70. ?????*?获取手机的硬件信息??
  71. [email protected]?
  72. ?????*/??
  73. ????private?String?getMobileInfo()?{??
  74. ????????StringBuffer?sb?=?new?StringBuffer();??
  75. ????????//通过反射获取系统的硬件信息???
  76. ????????try?{??
  77. ??
  78. ????????????Field[]?fields?=?Build.class.getDeclaredFields();??
  79. ????????????for(Field?field:?fields){??
  80. ????????????????//暴力反射?,获取私有的信息???
  81. ????????????????field.setAccessible(true);??
  82. ????????????????String?name?=?field.getName();??
  83. ????????????????String?value?=?field.get(null).toString();??
  84. ????????????????sb.append(name+"="+value);??
  85. ????????????????sb.append("\n");??
  86. ????????????}??
  87. ????????}?catch?(Exception?e)?{??
  88. ????????????e.printStackTrace();??
  89. ????????}??
  90. ????????return?sb.toString();??
  91. ????}??
  92. ??
  93. ????/**?
  94. ?????*?获取手机的版本信息?
  95. [email protected]?
  96. ?????*/??
  97. ????private?String?getVersionInfo(){??
  98. ????????try?{??
  99. ????????????PackageManager?pm?=?context.getPackageManager();??
  100. ?????????????PackageInfo?info?=pm.getPackageInfo(context.getPackageName(),?0);??
  101. ?????????????return??info.versionName;??
  102. ????????}?catch?(Exception?e)?{??
  103. ????????????e.printStackTrace();??
  104. ????????????return?"版本号未知";??
  105. ????????}??
  106. ????}??
  107. }??


创建一个Application实例将MyCrashHandler注册到整个应用程序上,创建出服务并进行传递:

[java]?view plaincopy
  1. /**?
  2. ?*?整个(app)程序初始化之前被调用??
  3. [email protected]?
  4. ?*?
  5. ?*/??
  6. public?class?DoubanApplication?extends?Application?{??
  7. ????public?NoteEntry?entry;??
  8. ????@Override??
  9. ????public?void?onCreate()?{??
  10. ????????super.onCreate();??
  11. ????????String?apiKey?=?"0fab7f9aa21f39cd2f027ecfe65dad67";??
  12. ????????String?secret?=?"87fc1c5e99bfa5b3";??
  13. ????????//?获取到service??
  14. ????????DoubanService?myService?=?new?DoubanService("我的小豆豆",?apiKey,??
  15. ????????????????secret);??
  16. ????????myService.setAccessToken("1fa4e5be0f808a0b5eeeb13a2e819e21",?"56a622c1138dbfce");??
  17. ????????MyCrashHandler?handler?=?MyCrashHandler.getInstance();??
  18. ????????handler.init(getApplicationContext(),myService);??
  19. ????????Thread.setDefaultUncaughtExceptionHandler(handler);??
  20. ????}??
  21. }??

?

  相关解决方案