android 出现异常重启

  1. 推荐方法:  
  2. public class HKApplication extends Application  
  3.     PendingIntent restartIntent;  
  4.   
  5.     @Override  
  6.     public void onCreate()  
  7.         super.onCreate();  
  8.   
  9.         // 以下用来捕获程序崩溃异常  
  10.         Intent intent new Intent();  
  11.         // 参数1:包名,参数2:程序入口的activity  
  12.         intent.setClassName("com.hk.shop", "com.hk.shop.WelcomeActivity");  
  13.         restartIntent PendingIntent.getActivity(getApplicationContext(), 0,  
  14.                 intent, Intent.FLAG_ACTIVITY_NEW_TASK);  
  15.         Thread.setDefaultUncaughtExceptionHandler(restartHandler); // 程序崩溃时触发线程  
  16.      
  17.   
  18.     public UncaughtExceptionHandler restartHandler new UncaughtExceptionHandler()  
  19.         @Override  
  20.         public void uncaughtException(Thread thread, Throwable ex)  
  21.             AlarmManager mgr (AlarmManager) getSystemService(Context.ALARM_SERVICE);  
  22.             mgr.set(AlarmManager.RTC, System.currentTimeMillis() 1000,  
  23.                     restartIntent); // 1秒钟后重启应用  
  24.             ActivityContrl.finishProgram(); // 自定义方法,关闭当前打开的所有avtivity  
  25.          
  26.     };  
  27.  
  28.   
  29. 将 HKApplication 在主配置文件中指定为你应用的入口  
  30.   
  31.   
  32. 这个异常的捕获也可以只写在某个activity中  
  33.   
  34.   
  35. 这个应用重启还是要慎用,一旦发生在首页,应用将不断重启,希望有人能给出好的解决办法。  
  36.   
  37.   
  38. 如果需要 ActivityContrl.finishProgram(); 
  39. 转自http://zheyiw.iteye.com/blog/1670609
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值