25 Android異常處理
(轉(zhuǎn)自:http://blog.csdn.net/zkw12358/article/details/11097649#)
Java中提供了UncaughtExceptionHandler這個(gè)接口,Android沿用了此接口,通過(guò)實(shí)現(xiàn)此接口,能夠處理線程被一個(gè)無(wú)法捕捉的異常所終止的情況。
ThreadGroup這個(gè)類就是實(shí)現(xiàn)了UncaughtExceptionHandler這個(gè)接口,如果想捕獲異常,可以實(shí)現(xiàn)這個(gè)接口或者繼承ThreadGroup,并重載uncaughtException方法。
在實(shí)現(xiàn)UncaughtExceptionHandler時(shí),必須重載uncaughtException(Threadthread, Throwable ex) ,如果沒(méi)有實(shí)現(xiàn)該接口,也就是沒(méi)有顯示捕捉異常,則ex為空,否則ex不為空,thread 則為出異常的線程。
如下代碼實(shí)現(xiàn)UncaughtExceptionHandler接口,顯示處理線程異常終止的情況:
public?class?UnCeHandler?implements?UncaughtExceptionHandler?{
private?Thread.UncaughtExceptionHandler?mDefaultHandler;
public?static?final?String?TAG?=?"CatchExcep";
CatchExcep?application;
public?UnCeHandler(CatchExcep?application){
//獲取系統(tǒng)默認(rèn)的UncaughtException處理器
mDefaultHandler?=?Thread.getDefaultUncaughtExceptionHandler();
this.application?=?application;
}
@Override
public?void?uncaughtException(Thread?thread,?Throwable?ex)?{
if(!handleException(ex)?&&?mDefaultHandler?!=?null){
//如果用戶沒(méi)有處理則讓系統(tǒng)默認(rèn)的異常處理器來(lái)處理
mDefaultHandler.uncaughtException(thread,?ex);
}else{
try{
Thread.sleep(2000);
}catch?(InterruptedException?e){
Log.e(TAG,?"error?:?",?e);
}
Intent?intent?=?new?Intent(application.getApplicationContext(),?MainActivity.class);
PendingIntent?restartIntent?=?PendingIntent.getActivity(
application.getApplicationContext(),?0,?intent,
Intent.FLAG_ACTIVITY_NEW_TASK);
//退出程序
AlarmManager?mgr?=?(AlarmManager)application.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC,?System.currentTimeMillis()?+?1000,
restartIntent);?//?1秒鐘后重啟應(yīng)用
application.finishActivity();
}
}
/**
*?自定義錯(cuò)誤處理,收集錯(cuò)誤信息?發(fā)送錯(cuò)誤報(bào)告等操作均在此完成.
*
*?@param?ex
*?@return?true:如果處理了該異常信息;否則返回false.
*/
private?boolean?handleException(Throwable?ex)?{
if?(ex?==?null)?{
return?false;
}
//使用Toast來(lái)顯示異常信息
new?Thread(){
@Override
public?void?run()?{
Looper.prepare();
Toast.makeText(application.getApplicationContext(),?"很抱歉,程序出現(xiàn)異常,即將退出.",
Toast.LENGTH_SHORT).show();
Looper.loop();
}
}.start();
return?true;
}
}
通過(guò)在androidApplication 這個(gè)全局類中處理異常:
public?class?CatchExcep?extends?Application{
ArrayList?list?=?new?ArrayList();
public?void?init(){
//設(shè)置該CrashHandler為程序的默認(rèn)處理器
UnCeHandler?catchExcep?=?new?UnCeHandler(this);
Thread.setDefaultUncaughtExceptionHandler(catchExcep);
}
/**
*?Activity關(guān)閉時(shí),刪除Activity列表中的Activity對(duì)象*/
public?void?removeActivity(Activity?a){
list.remove(a);
}
/**
*?向Activity列表中添加Activity對(duì)象*/
public?void?addActivity(Activity?a){
list.add(a);
}
/**
*?關(guān)閉Activity列表中的所有Activity*/
public?void?finishActivity(){
for?(Activity?activity?:?list)?{
if?(null?!=?activity)?{
activity.finish();
}
}
//殺死該應(yīng)用進(jìn)程
android.os.Process.killProcess(android.os.Process.myPid());
}
}
測(cè)試代碼:
Button?btn;
TextView?tv;
private?CatchExcep?application;
@Override
protected?void?onCreate(Bundle?savedInstanceState)?{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn?=?(Button)findViewById(R.id.btn);
tv?=?(TextView)findViewById(R.id.tv);
application?=?(CatchExcep)getApplication();
application.init();
application.addActivity(this);
btn.setOnClickListener(this);
}
/**
*?人為制造的異常*/
public?void?press(){
new?Thread(new?Runnable()?{
@Override
public?void?run()?{
tv.setText("dfsd");
}
}).start();
}
@Override
public?void?onClick(View?v)?{
press();
}
}