如何在App崩溃时执行一段代码
2017-11-13 14:16
267 查看
其实这个算是java的知识,在程序抛出无法捕捉的异常时,会触发Thread中的
当然Android中系统自己实现了这个接口,就是下面的
在App启动时,App会执行这个语句
这样Android就完全接管了不可捕捉的异常,也就是Crash,可以看到,Android的处理到最后是把当前进程给结束了,这就是Crash后App会闪退的原因了。
因此我们要自己处理Crash的话也需要自己实现一个
这样做的好处有很多,比如可以在这里将异常上传至服务器,也可以给用户有好的提示,或者是掩盖错误,直接返回到出错之前的页面。
defaultUncaughtExceptionHandler的
uncaughtException方法,
UncaughtExceptionHandler就是下面这个接口
public interface UncaughtExceptionHandler { /** * Method invoked when the given thread terminates due to the * given uncaught exception. * <p>Any exception thrown by this method will be ignored by the * Java Virtual Machine. * @param t the thread * @param e the exception */ void uncaughtException(Thread t, Throwable e); }
当然Android中系统自己实现了这个接口,就是下面的
KillApplicationHandler
/** * Handle application death from an uncaught exception. The framework * catches these for the main threads, so this should only matter for * threads created by applications. Before this method runs, * {@link LoggingHandler} will already have logged details. */ private static class KillApplicationHandler implements Thread.UncaughtExceptionHandler { public void uncaughtException(Thread t, Throwable e) { try { // Don't re-enter -- avoid infinite loops if crash-reporting crashes. if (mCrashing) return; mCrashing = true; // Try to end profiling. If a profiler is running at this point, and we kill the // process (below), the in-memory buffer will be lost. So try to stop, which will // flush the buffer. (This makes method trace profiling useful to debug crashes.) if (ActivityThread.currentActivityThread() != null) { ActivityThread.currentActivityThread().stopProfiling(); } // Bring up crash dialog, wait for it to be dismissed ActivityManager.getService().handleApplicationCrash( mApplicationObject, new ApplicationErrorReport.ParcelableCrashInfo(e)); } catch (Throwable t2) { if (t2 instanceof DeadObjectException) { // System process is dead; ignore } else { try { Clog_e(TAG, "Error reporting crash", t2); } catch (Throwable t3) { // Even Clog_e() fails! Oh well. } } } finally { // Try everything to make sure this process goes away. Process.killProcess(Process.myPid()); System.exit(10); } } }
在App启动时,App会执行这个语句
Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler());
这样Android就完全接管了不可捕捉的异常,也就是Crash,可以看到,Android的处理到最后是把当前进程给结束了,这就是Crash后App会闪退的原因了。
finally { // Try everything to make sure this process goes away. Process.killProcess(Process.myPid()); System.exit(10); }
因此我们要自己处理Crash的话也需要自己实现一个
UncaughtExceptionHandler,然后在Application类中进行设置
class App : Application() { override fun onCreate() { super.onCreate() Thread.setDefaultUncaughtExceptionHandler(MyUncaughtExceptionHandler()); }
这样做的好处有很多,比如可以在这里将异常上传至服务器,也可以给用户有好的提示,或者是掩盖错误,直接返回到出错之前的页面。
相关文章推荐
- ASP.NET(C#) 如何定时执行一段代码?
- ASP.NET(C#) 如何定时执行一段代码?
- 如何查看iOS已上架app的崩溃情况以及定位crash代码行
- VC++如何计算一段代码的执行时间
- ASP.NET(C#) 如何定时执行一段代码?
- 如何快速的查看一段代码的执行时间
- ios app崩溃的时候如何通过代码层面确定崩溃位置
- IAR下如何确定某一段代码的执行时间
- ASP.NET(C#) 如何定时执行一段代码
- 如何让页面刷新前执行一段js代码
- 如何让ajax执行完在执行下一段代码
- 如何象SQL语句中的Exec那样在VB中执行一段代码?
- delphi如何获得一段代码执行消耗的时间
- 如何象SQL语句中的Exec那样在VB中执行一段代码?
- 用GetTickCount()计算一段代码执行耗费的时间的小例子
- 用一行代码让w3wp进程崩溃,如何查找w3wp进程崩溃的原因
- 如何在VB中操作EXCEL(一段代码,两个可以使用的过程)
- 如何在服务器(Linux)里面定时(crontab)执行需要虚拟环境(venv)的代码(爬虫)
- Net 如何计算一段代码的效率
- 如何通过崩溃地址找到出错的代码行(ZT)