Android Force Close和ANR等异常处理方法
来源:互联网 发布:godaddy域名赎回后解析 编辑:程序博客网 时间:2024/05/16 05:01
对android应用而言最常出现的异常是Force close和ANR(Application is not response).
对于这两类错误而言,应用是可以进行相关处理的。
一 Forceclose这类问题主要通过Thread.UncaughtExceptionHandler这个类来捕获异常。通过实现类里面的方法uncaughtException来实现应用在捕获到异常后进行相关的处理。一般这里处理基本放在应用的Application类中。为了方便大家进行相关处理,我这里写了个类,大家直接在Application回调即可。
new ExceptionHandler(mContext).setFCListener(new ExceptionHandler.FCListener() { @Override public void onFCDispose(Throwable paramThrowable) { Log.d(TAG, onFCListerner enter!!!); new Thread(){ public void run(){ Looper.prepare(); Toast.makeText(mContext, APP is Force Close do what you want!, Toast.LENGTH_LONG).show(); Looper.loop(); } }.start(); } });
同样的对于ANR问题,应用也可以做相关处理。对ANR,我们可以这样处理。通过一个看门狗来实时的检测主线程,一旦主线程发生阻塞,则通知Application 做相关处理。
主要方法是在线程中每隔一段时间(Activity一般是5S,广播一般是10S),向主线程发送一个messager,使计数器加1,如果到点没有加1,则表明主线程阻塞。
@Override public void run() { setName(|ANR-WatchDog|); int lastTick; while (!isInterrupted()) { lastTick = mTick; mUIHandler.post(tickerRunnable); try { Thread.sleep(mTimeoutInterval); } catch (InterruptedException e) { mInterruptionListener.onInterrupted(e); return ; } // If the main thread has not handled _ticker, it is blocked. ANR. if (mTick == lastTick) { ANRError error; if (mNamePrefix != null) error = ANRError.New(mNamePrefix, mLogThreadsWithoutStackTrace); else error = ANRError.NewMainOnly(); mAnrListener.onAppNotResponding(error); return ; } } }
private final Runnable tickerRunnable = new Runnable() { @Override public void run() { mTick = (mTick + 1) % 10; } };
0 0
- Android Force Close和ANR等异常处理方法
- Android Force Close和ANR等异常处理方法
- Android开发的ANR和Force Close
- 关于ANR和Force Close
- ANR和Force close产生和原因
- ANR&force close
- ANR,Force Close
- Android两种常见错误-ANR和FC(Force close)
- android-ANR异常处理
- Android ANR、Force Closed
- Android ANR问题介绍和处理方法
- android ANR处理方法
- Android系统ANR以及Force Close出现的原因以及解决办法
- Force close
- Android Force close是什么引起的?
- Android anr异常测试
- Android ANR异常
- android ANR异常解决方法
- UVA - 455 Periodic Strings
- uva 1455 王国
- Handler、Looper、Message源码解析
- Hadoop的发行版本介绍
- python线程锁实践实例
- Android Force Close和ANR等异常处理方法
- UVA - 1225 Digit Counting
- android addContentView视图后的移除
- GitBash+GitLab+Eclipse使用攻略非常详细版
- UVA - 1585 Score
- Shell脚本中执行sql语句操作mysql的5种方法
- 一目了然的二分查找
- Mybatis分页插件
- oracle如何创建Database Link