- 推薦方法:
- 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秒鐘後重啓應用
-
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1); - }
- };
- }
- 將 HKApplication 在主配置文件中指定爲你應用的入口
- 這個異常的捕獲也可以只寫在某個activity中
-
- 這個應用重啓還是要慎用,一旦發生在首頁,應用將不斷重啓,希望有人能給出好的解決辦法。
-
android 崩潰 重啓 捕獲異常 並重啓應用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.