Android 检测程序异常崩溃,重启应用
16lz
2021-01-23
程序实时监测,程序异常 注意:Application 需在清单文件中注册
package com.catch.catchex;import java.util.ArrayListimport com.catch.catchex.CashHandler ;import android.app.Activity;import android.app.Application;import android.content.Context;public class MyApplication extends Application { private static Context context; ArrayList list = new ArrayList(); @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); } /** * context */ public static Context getContext() { return context; } public void init() { // 设置该CashHandler为程序的默认处理器 CashHandler unCeHandler = new CashHandler(this); Thread.setDefaultUncaughtExceptionHandler(unCeHandler); } /** * @param activity activity关闭时,删除Activity列表中的Activity对象 */ public void removeActivity(Activity activity) { list.remove(activity); } /** * @param activity 向列表中添加Activity对象 */ public void addActivity(Activity activity) { list.add(activity); } /** * @param activity 关闭Activity列表中的 所有Activity */ public void finishActivity() { for (Activity activity : list) { if (null != activity) { activity.finish(); } } // 杀死应用进程 android.os.Process.killProcess(android.os.Process.myPid()); }}
异常处理器 CashHandler
package com.catch.catchex;import java.lang.Thread.UncaughtExceptionHandler;import android.app.AlarmManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Looper;import android.util.Log;import android.widget.Toast;import com.catch.catchex.MainActivity;import com.catch.catchex.MyApplication;public class CashHandler implements UncaughtExceptionHandler { private UncaughtExceptionHandler mDefaultHandler; public final String TAG = "CatchExcep"; MyApplication application; public CashHandler(MyApplication myApplication){ // // 获取系统默认的UncaughtException处理器 mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler(); this.application = myApplication; } @Override public void uncaughtException(Thread thread, Throwable ex) { // TODO Auto-generated method stub if (!handlerException(ex) && mDefaultHandler != null) { // 若果用户没有处理则让系统默认的异常处理器来处理 mDefaultHandler.uncaughtException(thread, ex); } else { try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block Log.e(TAG, "error ;", e); } Intent intent = new Intent(application.getApplicationContext(), MainActivity.class); PendingIntent restartIntent = PendingIntent.getActivity(application.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK); // 退出程序 AlarmManager mgr = (AlarmManager) application.getSystemService(Context.ALARM_SERVICE); mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒后重启 application.finishActivity(); } } /** * 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. * * @param ex * @return true:如果处理了该异常信息;否则返回false. */ private boolean handlerException(Throwable ex) { // TODO Auto-generated method stub if (ex == null) { return false; } // 发个友好的提示,弹个土司 new Thread() { public void run() { Looper.prepare(); Toast.makeText(application.getApplicationContext(), "检测到程序异常,即将退出", Toast.LENGTH_SHORT).show(); Looper.loop(); }; }.start(); return true; }}
MainActivity 中初始化
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CatchExcApplication(); // 检测程序异常 initView(); // 初始化View } /** * 检测程序异常,重启 */ public void CatchExcApplication() { // 添加程序异常检测 MyApplication application = (MyApplication) getApplication(); application.init(); application.addActivity(this); }
清单文件配置 MyApplication
更多相关文章
- Android应用程序获取ROOT权限代码
- android交叉编译c程序
- Android应用程序键盘(Keyboard)消息处理机制分析(23)
- Android判断程序是否第一次启动
- Android 返回键双击退出程序
- Android应用程序窗口(Activity)的测量(Measure)、布局(Layout)和绘制(Dr
- Android应用程序窗口(Activity)的测量(Measure)、布局(Layout)和绘制(Dr
- Android应用程序组成部分
- adb shell 命令查看android 应用程序 创建的 sqlite 数据库