博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android程序意外Crash后自动重启
阅读量:4706 次
发布时间:2019-06-10

本文共 3380 字,大约阅读时间需要 11 分钟。

 

1、自定义UncaughtExceptionHandler

 

1 public class UnCeHandler implements UncaughtExceptionHandler { 2      3     private Thread.UncaughtExceptionHandler mDefaultHandler; 4     public final String TAG = "CatchExcep"; 5     CatchExcApplication application; 6  7     public UnCeHandler(CatchExcApplication application) { 8         // 获取系统默认的UncaughtException处理器 9         mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();10         this.application = application;11     }12 13     @Override14     public void uncaughtException(Thread thread, Throwable ex) {15         if (!handleException(ex) && mDefaultHandler != null) {16             // 如果用户没有处理则让系统默认的异常处理器来处理17             mDefaultHandler.uncaughtException(thread, ex);18         } else {19             try {20                 Thread.sleep(2000);21             } catch (InterruptedException e) {22                 Log.e(TAG, "error : ", e);23             }24             Intent intent = new Intent(application.getApplicationContext(),MainActivity.class);25             PendingIntent restartIntent = PendingIntent.getActivity(  26                     application.getApplicationContext(), 0, intent,27                     Intent.FLAG_ACTIVITY_NEW_TASK);28             // 退出程序29             AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE);30             mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,restartIntent); // 1秒钟后重启应用31             application.finishActivity();32         }33     }34 35     /**36      * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.37      * 38      * @param ex39      * @return true:如果处理了该异常信息;否则返回false.40      */41     private boolean handleException(Throwable ex) {42         if (ex == null) {43             return false;44         }45         // 使用Toast来显示异常信息46         new Thread() {47             @Override48             public void run() {49                 Looper.prepare();50                 Toast.makeText(application.getApplicationContext(),"很抱歉,程序出现异常,即将退出.", Toast.LENGTH_SHORT).show();51                 Looper.loop();52             }53         }.start();54         return true;55     }

 

2、自定义Application

1 public class CatchExcApplication extends Application { 2  3     ArrayList
list = new ArrayList
(); 4 5 public CatchExcApplication() 6 { 7 //设置该CrashHandler为程序的默认处理器 8 Thread.setDefaultUncaughtExceptionHandler(new UnCeHandler(this)); 9 }10 11 14 15 /** 16 * Activity关闭时,删除Activity列表中的Activity对象*/ 17 public void removeActivity(Activity a){ 18 list.remove(a); 19 } 20 21 /** 22 * 向Activity列表中添加Activity对象*/ 23 public void addActivity(Activity a){ 24 list.add(a); 25 } 26 27 /** 28 * 关闭Activity列表中的所有Activity*/ 29 public void finishActivity(){ 30 for (Activity activity : list) { 31 if (null != activity) { 32 activity.finish(); 33 } 34 } 35 //杀死该应用进程 36 android.os.Process.killProcess(android.os.Process.myPid()); 37 }38 }

 

3、配置AndroidManifest.xml

1  
7
10
11
12 13
14
15
16

 

转载于:https://www.cnblogs.com/l2rf/p/4410092.html

你可能感兴趣的文章
Sqlserver 系统视图简单说明
查看>>
【摘录】PHP异步调用实现方式
查看>>
php缓存机制
查看>>
bzoj2049 线段树 + 可撤销并查集
查看>>
sql语句---存在即更新,否则insert
查看>>
cookie机制、session机制
查看>>
BZOJ 3787: Gty的文艺妹子序列
查看>>
Comet OJ - Contest #5 简要题解
查看>>
CF1093G Multidimensional Queries
查看>>
移动端提升页面速度与网站性能
查看>>
深度学习中优化【Normalization】
查看>>
POJ2309BST(树状数组)
查看>>
洛谷P2114 起床困难综合症【位运算】【贪心】
查看>>
net 把指定 URI 的资源下载到本地
查看>>
一个关于session的问题
查看>>
加快开发时间的8个CSS的预处理程序
查看>>
dom元素高度、屏幕高度 获取
查看>>
asp.net 设置session失效时间
查看>>
杭电多校第四场 E Matrix from Arrays
查看>>
ReactiveCocoa操作方法-线程\时间
查看>>