`
zheyiw
  • 浏览: 999476 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

android 崩溃 重启 捕获异常 并重启应用

阅读更多
推荐方法:
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();
请在本博客中寻找,我记得写了笔记

分享到:
评论
3 楼 lvshandong 2015-02-02  
可以加下你QQ吗,有问题讨论
2 楼 qishaohe 2013-09-23  
我怎么跟你一样的代码怎么就是不行呢?能否告诉一下你的QQ
1 楼 cawn 2012-11-08  
angellcawn mark标记

相关推荐

Global site tag (gtag.js) - Google Analytics