- 推薦方法:
- public class HKApplication extends Application {
- PendingIntent restartIntent;
-
- @Override
- public void onCreate() {
- super.onCreate();
-
- // 以下用來捕獲程序崩潰異常
- Intent intent = new Intent();
- // 參數(shù)1:包名,參數(shù)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); // 程序崩潰時觸發(fā)線程
- }
-
- 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秒鐘后重啟應(yīng)用
- ActivityContrl.finishProgram(); // 自定義方法,關(guān)閉當(dāng)前打開的所有avtivity
- }
- };
- }
-
- 將 HKApplication 在主配置文件中指定為你應(yīng)用的入口
-
-
- 這個異常的捕獲也可以只寫在某個activity中
-
-
- 這個應(yīng)用重啟還是要慎用,一旦發(fā)生在首頁,應(yīng)用將不斷重啟,希望有人能給出好的解決辦法。
-
-
- 如果需要 ActivityContrl.finishProgram();
- 請在本博客中尋找,我記得寫了筆記
|