android处理unchecked exception
来源:互联网 发布:c语言else if成绩判定 编辑:程序博客网 时间:2024/06/18 09:44
使用Android提供的Thread.UncaughtExceptionHandler类,可以捕获系统的unchecked exception,并提供相应的错误处理操作,增加程序的健壮性。
一个简单的示例:
public class CrashHandle implements Thread.UncaughtExceptionHandler { private static CrashHandle Instance = new CrashHandle(); private Context context; private Thread.UncaughtExceptionHandler DefeaultHandler; private CrashHandle(){} //单例模式 public static CrashHandle getInstance(){ return Instance; } public void init(Context context){ this.context = context; DefeaultHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); } @Override public void uncaughtException(Thread thread, Throwable ex) { // TODO Auto-generated method stub if((!HandlerException(ex)) && (this.DefeaultHandler != null)){ this.DefeaultHandler.uncaughtException(thread, ex); }else{ try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Process.killProcess(Process.myPid()); System.exit(1); } } //自定义错误处理,收集错误信息与发送错误信息 public boolean HandlerException(Throwable ex){ if(ex == null){ return false; } new Thread(){ public void run(){ Looper.prepare(); Toast.makeText(context, "程序崩溃了,即将退出", 0).show(); Looper.loop(); } }.start(); return true; }}
因为Android程序的入口是application,为了使用该方法,我们需要继承Android的application类,在oncreat()方法初始化改类。
public class MyApplication extends Application{ private static MyApplication myApplication; public void onCreate(){ super.onCreate(); CrashHandle crash = CrashHandle.getInstance(); crash.init(getApplicationContext()); } public static MyApplication getMyApplication(){ if(myApplication == null){ myApplication = new MyApplication(); } return myApplication; }}
最后,在manifest里完成注册:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:name="com.zl.crash.MyApplication"/>
在mainactivity里制造一个unchecked exception,会看到如下效果:
1 0
- android处理unchecked exception
- java 线程中unchecked exception处理
- unchecked Exception & checked Exception
- unchecked Exception & checked Exception
- Java异常处理原则,check and Unchecked Exception
- Java异常处理原则,check and Unchecked Exception
- Exception(checked VS unchecked)
- Checked vs UnChecked Exception
- checked 和 unchecked exception
- Checked、Unchecked Exception
- checked & unchecked exception
- check and Unchecked Exception
- Checked exception vs Unchecked exception
- checked exception and unchecked exception
- checked exception和unchecked exception
- Checked Exception and UnChecked Exception
- Unchecked Exception 和 Checked Exception
- 异常管理 - Java异常处理分析 (Unchecked Exception VS Checked Exception)
- (function($){...})(jQuery)的意思
- Apache POI 设置宽度和行高
- Google推Android新开发语言Sky:流畅度 秒iOS
- 全面解析ECMAScript 6模块系统
- 如何创建和启动Django项目
- android处理unchecked exception
- 【Quartz】 JobListener、Triggerlistener、SchedulerListener介绍与使用
- office基本宏命令
- Android 一些没有关注过的类 ,陆续更新。。。
- 多重继承下的类作用域
- javaBean简介及基本使用
- oracle10g/11g 新特性五(用Shceduler自动化)
- Go语言使用Scp传输文件
- canvas时钟