Android 全局异常处理之UncaughtExceptionHandler
来源:互联网 发布:软件测试报告模板 编辑:程序博客网 时间:2024/06/07 11:16
在日常开发中可能有需要将机器奔溃日志保存本机,以便保存到本地。那么该如何做呢
实现UncaughtExceptionHandler接口
public class ApplicationCrashHandler implements Thread.UncaughtExceptionHandler { private static final String TAG = "ApplicationCrashHandler"; private Context context; private Thread.UncaughtExceptionHandler defalutHandler; /** * 异常处理初始化 * * @param context */ public void init(Context context) { this.context = context; // 获取系统默认的UncaughtException处理器 defalutHandler = Thread.getDefaultUncaughtExceptionHandler(); // 设置该CrashHandler为程序的默认处理器 Thread.setDefaultUncaughtExceptionHandler(this); } /** * 当UncaughtException发生时会转入该函数来处理 */ @Override public void uncaughtException(Thread thread, Throwable ex) { // 自定义错误处理 boolean res = handleException(ex); if (!res && defalutHandler != null) { // 如果用户没有处理则让系统默认的异常处理器来处理 defalutHandler.uncaughtException(thread, ex); } else { try { Thread.sleep(3000); } catch (InterruptedException e) { Log.e(TAG, "error : ", e); } ex.printStackTrace(); // 退出程序 android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } } /** * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. * * @param ex * @return true:如果处理了该异常信息;否则返回false. */ private boolean handleException(final Throwable ex) { if (ex == null) { return false; } //输出带行数的具体错误信息 String errorStr= Log.getStackTraceString(ex);//直接将该信息直接写入文件即可 Log.e(TAG, "error : " + errorStr); return true; }}
注册UncaughtExceptionHandler接口
public class MyApplication extends Application { private static final String TAG = "MyApplication"; @Override public void onCreate() { super.onCreate(); //注册代码 ApplicationCrashHandler crashHandler = new ApplicationCrashHandler(); crashHandler.init(getApplicationContext()); }}
或者在Activity的onCreate
里面注册也行。
友盟错误统计上传冲突
集成了友盟错误统计,如果注册的位置不当会发现打印的信息被拦截或者无信息输出.那么该如何解决呢。
1.关闭友盟错误统计
MobclickAgent.setCatchUncaughtExceptions(false);
2.在初始化友盟前注册(App)
总结
以上模式推荐在开发中使用。
0 0
- Android 全局异常处理之UncaughtExceptionHandler
- android UncaughtExceptionHandler全局异常处理
- 全局异常处理UncaughtExceptionHandler
- Android 之捕获全局异常之UncaughtExceptionHandler
- Android异常处理——UncaughtExceptionHandler捕获全局异常
- android 处理程序全局异常和错误,UncaughtExceptionHandler
- 学习笔记之Android利用UncaughtExceptionHandler捕获全局异常
- android 程序错误处理全局处理 UncaughtExceptionHandler
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- Android使用UncaughtExceptionHandler捕获全局异常
- UserWarning: .python-eggs is writable by group/others and vulnerable to attack when used with get_r
- C++11实现自旋锁
- 用javascript实现多表单提交同一信息
- 心好,一切都好
- eclipse:修改JVM运行内存
- Android 全局异常处理之UncaughtExceptionHandler
- UVA - 321 The New Villa
- Android网络操作和优化相关
- window.open不被拦截
- oracle中行转列语句参考
- 透明度和zoom属性
- 一些小知识点2
- 设计模式之观察者模式
- 互斥锁,自旋锁与自适应自旋锁