android程序奔溃异常捕捉
来源:互联网 发布:淘宝官方下载电脑版 编辑:程序博客网 时间:2024/04/30 14:01
我们的程序虽然在发布之前都会经过严格测试,但是总会有一个bug导致奔溃,这时为了将这个bug记录下来,如果有机会也可以把这些奔溃日志发给我们,做进一步的处理.
一、异常捕捉
- 要对程序的异常进行捕获,必须重写UncaughtExceptionHandler,如下:
public class MyUncaughtException implements UncaughtExceptionHandler {private Context mContext;private UncaughtExceptionHandler mDefaultHandler;@Overridepublic void uncaughtException(Thread thread, Throwable ex) {// TODO Auto-generated method stubif (!handleException(thread, ex) && mDefaultHandler != null) {mDefaultHandler.uncaughtException(thread, ex);} else {try {Thread.sleep(3000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}//默认处理mDefaultHandler.uncaughtException(thread, ex);
<span style="white-space:pre"></span>android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); // 重新启动程序,注释上面的退出程序 Intent intent = new Intent(); intent.setClass(mContext,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid());}//单例模式保证private MyUncaughtException() {}private static MyUncaughtException instance = null;public static MyUncaughtException getInstance() {if(instance == null) {instance = new MyUncaughtException();}return instance;}//初始化public void init(Context context) {mContext = context;mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();Thread.setDefaultUncaughtExceptionHandler(this);}//异常处理,保存错误信息,发送等操作private boolean handleException(Thread thread, Throwable e) {if(e == null) {return false;}final String error = e.getLocalizedMessage();//显示异常信息new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubLooper.prepare();String description = "程序出错了";if(error != null) {description += error;}Toast.makeText(mContext, description, Toast.LENGTH_SHORT).show();Looper.loop();super.run();}}.start();//保存错误信息saveCrashLog();return true;}//这里暂不写实现了private void saveCrashLog() {}
- 需要在Application中注册,用来监控整个程序
public class MyApp extends Application {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();MyUncaughtException exceptionHandler = MyUncaughtException.getInstance();exceptionHandler.init(getApplicationContext());}}
- 修改androidManifest.xml中application的name
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:name="MyApp"
4. 始终不理解的是为什么要使用线程处理,为什么将线程升级为looper线程?
如果主线程发生uncaughtException 通常会导致main thread 的looper 退出,这时不会再响应任何UI消息,所以要在其他线程中调用显示的方法。
一直没有找到这地方的源码,猜测,在调用Toast.show()方法的时候,最后会形成一条消息Message,并且如果当前线程没有Handler,会自动构造一个默认的Handler,进行处理,最后这个消息发送Handler对应的Loooper,所以,这个线程一定要是Looper线程,而且在Toast之前,Looper线程已经调用Looper.prapare();
0 0
- android程序奔溃异常捕捉
- android程序奔溃异常捕捉
- android 程序异常捕捉处理
- Android 对程序异常崩溃的捕捉
- android开发之程序异常捕捉处理
- Android 对程序异常崩溃的捕捉
- Android 对程序异常崩溃的捕捉
- Android程序异常捕捉并上传服务器
- Android 对程序异常崩溃的捕捉
- android 捕捉异常
- Android全局异常捕捉
- Android 对全局程序异常崩溃的捕捉
- Android 程序异常崩溃的捕捉 前后端
- android 捕捉 异常 崩溃 捕捉 crash
- Android捕捉未捕捉的异常
- Android全局异常捕捉器
- android 应用程序异常崩溃捕捉
- android异常捕捉处理-CrashHandler
- Struts2中Action配置中的各项默认值
- 传记
- 个人学习-Struts2
- ObjectARX学习笔记(十九)--如何设置扩展字典的数据AcDbDictionary
- 关于Windows高DPI
- android程序奔溃异常捕捉
- C#中双问号(??)在检测Null方面的带来的方便
- 分布式文件系统(未完)
- Kali Linux 安全渗透教程<第六更>1.4.2 安装至USB驱动器Kali Linux
- Android清理设备内存详细完整示例(一)
- Android MediaPlayer架构图解
- MySql安装后各种错误解决办法
- 细说C++覆盖方法和重载方法
- 条款23:宁以no-member、no_friend替换member函数