推荐方法:
public class HKApplication extends Application {
PendingIntent restartIntent;

@Override
public void onCreate() {
super.onCreate();

// 以下用来捕获程序崩溃异常
Intent intent = new Intent();
// 参数1:包名,参数2:程序入口的activity
intent.setClassName("com.hk.shop", "com.hk.shop.WelcomeActivity");
restartIntent = PendingIntent.getActivity(getApplicationContext(), 0,
intent, Intent.FLAG_ACTIVITY_NEW_TASK);
Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程
}

public UncaughtExceptionHandler restartHandler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000,
restartIntent); // 1秒钟后重启应用
ActivityContrl.finishProgram(); // 自定义方法,关闭当前打开的所有avtivity
}
};
}

将 HKApplication 在主配置文件中指定为你应用的入口


这个异常的捕获也可以只写在某个activity中


这个应用重启还是要慎用,一旦发生在首页,应用将不断重启,希望有人能给出好的解决办法。


如果需要 ActivityContrl.finishProgram();
请在本博客中寻找,我记得写了笔记

更多相关文章

  1. Appium的DesiredCapabilities参数设置
  2. 开发Android应用程序来使用硬件访问服务
  3. Android 在程序中重启APP的方法
  4. Android之Handler的几个例子程序,对比学习
  5. 程序员之路
  6. Android 网络编程基础之简单聊天程序
  7. Android中启动第三方程序的代码

随机推荐

  1. 安卓自学,手机上的横竖屏切换,状态栏隐藏
  2. Xcode 4.2 编译 Android
  3. 进程博客纳入
  4. Android(安卓)- 销毁指定Activity
  5. android常用控件(二)
  6. 基本Dalvik VM调用
  7. Android AVD启动失败
  8. Android签名验证简介
  9. Android(安卓)webview 常用配置
  10. Android原生运行uniapp使用5+app创建项目