Android捕获全局异常,并对异常做出处理
2012-02-24 15:52
501 查看
在做项目时,经常会把错误利用异常抛出去,这样在开发时就可以通过手机抛的异常排查错误,很方便。但是当程序开发完毕,版本稳定,需要上线时,为了避免抛出异常影响用户感受,可以捕获全局异常,对异常做出处理。
具体的实方法如下:
利用Thread.UncaughtExceptionHandler 获取异常,并对异常做出处理:
public class MyUncaughtExceptionHandler implements
Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler a;
MyUncaughtExceptionHandler(){
this.a = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.i("huilurry","ppppppppppppp="+ex.getMessage());
//是否抛出异常
// if(a!=null)
// a.uncaughtException(thread, ex);
}
}
具体调用:
public class HuiLurryActivty extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String t=android.provider.Settings.System.getString(getContentResolver(), "android_id");
Log.i("huilurry","android_id="+t);
huilurry();
throw new NullPointerException("is null");
}
HandlerThread localHandlerThread;
Handler handler;
private void huilurry()
{
localHandlerThread=new HandlerThread("huilurry");
localHandlerThread.start();
handler=new Handler(localHandlerThread.getLooper());
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
}
}
主要是利用了Hander和HandlerThread。
源代码见:http://wangjun-memory.googlecode.com/svn/trunk/android.huilurry
http://blog.csdn.net/l_serein/article/details/6909037
具体的实方法如下:
利用Thread.UncaughtExceptionHandler 获取异常,并对异常做出处理:
public class MyUncaughtExceptionHandler implements
Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler a;
MyUncaughtExceptionHandler(){
this.a = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
Log.i("huilurry","ppppppppppppp="+ex.getMessage());
//是否抛出异常
// if(a!=null)
// a.uncaughtException(thread, ex);
}
}
具体调用:
public class HuiLurryActivty extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String t=android.provider.Settings.System.getString(getContentResolver(), "android_id");
Log.i("huilurry","android_id="+t);
huilurry();
throw new NullPointerException("is null");
}
HandlerThread localHandlerThread;
Handler handler;
private void huilurry()
{
localHandlerThread=new HandlerThread("huilurry");
localHandlerThread.start();
handler=new Handler(localHandlerThread.getLooper());
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
}
}
主要是利用了Hander和HandlerThread。
源代码见:http://wangjun-memory.googlecode.com/svn/trunk/android.huilurry
http://blog.csdn.net/l_serein/article/details/6909037
相关文章推荐
- android捕获全局异常,并对异常做出处理
- android捕获全局异常,并对异常做出处理
- android捕获全局异常,并对异常做出处理
- android捕获全局异常,并对异常做出处理
- android捕获全局异常,并对异常做出处理
- Android捕获全局异常,并对异常做出处理
- Android处理未捕获的异常(应用全局异常)
- Android全局捕获异常处理
- Android应用捕获全局异常自定义处理
- Android全局异常处理(捕获异常,不弹出程序崩溃)
- Android的异常处理 全局的异常捕获
- Android 程序错误处理全局处理-捕获全局异常
- Android中处理崩溃异常 Android捕获全局异常信息并实现上传
- android应用开发-------------应用崩溃全局异常捕获处理(UncaughtExceptionHandler)
- Xamarin.Android-捕获未处理异常(全局异常)
- android apk 捕获全局异常处理,阻止弹出系统强制退出对话框
- Android全局异常处理(捕获异常,不弹出程序崩溃)
- Android 捕获全局异常信息并进行相关处理
- 详解Android全局异常的捕获处理
- c# android 全局捕获未处理异常