app崩溃后自动重启
2017-10-10 11:03
141 查看
android
引用:http://blog.csdn.net/caiwenfeng_for_23/article/details/41184353package com.tan.abnormalrestart;import java.lang.Thread.UncaughtExceptionHandler;import android.app.Application;import android.content.Intent;public class AppContext extends Application { protected static AppContext instance; public void onCreate() { super.onCreate(); instance = this; Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程 以下用来捕获程序崩溃异常 } // 创建服务用于捕获崩溃异常 private UncaughtExceptionHandler restartHandler = new UncaughtExceptionHandler() { public void uncaughtException(Thread thread, Throwable ex) { restartApp();//发生崩溃异常时,重启应用 } }; public void restartApp(){ Intent intent = new Intent(instance,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); instance.startActivity(intent); android.os.Process.killProcess(android.os.Process.myPid()); //结束进程之前可以把你程序的注销或者退出代码放在这段代码之前 }}
iOS
引用:http://blog.sina.com.cn/s/blog_b71d24920101ky2d.html在程序启动时加上一个异常捕获监听,用来处理程序崩溃时的回调动作
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
官方文档介绍:Sets the top-level error-handling function where you can perform last-minute logging before the program terminates.
UncaughtExceptionHandler是一个函数指针,该函数需要我们实现,可以取自己想要的名字。当程序发生异常崩溃时,该函数会得到调用,这跟C,C++中的回调函数的概念是一样的。
实现自己的处理函数
void UncaughtExceptionHandler(NSException exception) { NSArray arr = [exception callStackSymbols];//得到当前调用栈信息 NSString reason = [exception reason];//非常重要,就是崩溃的原因 NSString name = [exception name];//异常类型 NSLog(@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, arr); }
以上代码很简单,但是带来的作用是非常大的。
相关文章推荐
- app崩溃后捕获异常或自动重启
- Android开发之app崩溃后捕获异常或自动重启
- 【Android】App应用崩溃(Crash/Force Close)之后如何让它自动重启?
- app崩溃后捕获异常或自动重启
- app崩溃后捕获异常或自动重启
- android app崩溃后自动重启
- app崩溃后捕获异常或自动重启
- APP崩溃后自动重启
- Android APP崩溃上传日志到服务器并且重启!
- Android5.1.1源码 - App服务进程被杀后自动重启的原因
- 自动更新Android应用后, app 进入后台重进会重启
- 程序崩溃自动重启和未捕获到的异常写退出栈
- Qt浅谈之二十App自动重启及关闭子窗口
- linux下监视进程 崩溃挂掉后自动重启的shell脚本
- python脚本自动监视tomcat进程,如果崩溃就自动重启
- Android App崩溃上传日志到服务器并且重启!
- Android 开机自动重启App代码
- 用monit监控mongodb,崩溃后自动重启mongdb
- Android APP崩溃上传日志到服务器并且重启!
- android app在系统重启绕过USB授权对话框,自动获取USB权限