久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

Android 應用Crash 后自動重啟的方法小結

瀏覽:5日期:2022-09-23 16:40:18

前提

首先,我們肯定要在Application里面注冊一個CrashHandler,監聽應用crash

public class TestApplication extends MultiDexApplication { private static TestApplication mInstance; @Override public void onCreate() { super.onCreate(); Thread.setDefaultUncaughtExceptionHandler(new CrashHandler()); }

然后在這個CrashHandler 想辦法重啟應用。有兩種方法如下:

方法1.通過AlarmManager

public class CrashHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { //重啟app /** * 這種方式 功能是可以達成 * 但是有問題就是如果說你的app掛了 這時候會顯示系統桌面 * 然后你的app有啟動起來了 * 給人的感覺不太好 */ Intent intent = new Intent(); Context context = TestApplication.getInstance(); intent.setClass(context, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_ONE_SHOT); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC,System.currentTimeMillis() + 100,pendingIntent); Process.killProcess(Process.myPid()); System.exit(0); }}

方法2:

使用第三方庫

implementation ’com.jakewharton:process-phoenix:2.0.0’

public class CrashHandler implements Thread.UncaughtExceptionHandler { @Override public void uncaughtException(Thread t, Throwable e) { ProcessPhoenix.triggerRebirth(TestApplication.getInstance()); }}

這個第三方庫的原理是:當app 崩潰的時候,ProcessPhoenix.triggerRebirth(TestApplication.getInstance());就會觸發啟動另外一個進程的Activity,然后把當前崩潰的進程結束掉。在新進程的Activity里面,把應用在自己的進程里面的啟動起來。

總結

到此這篇關于Android 應用Crash 后自動重啟的文章就介紹到這了,更多相關android 自動重啟內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 扬中市| 循化| 桃园县| 陆河县| 洪湖市| 太仆寺旗| 凭祥市| 申扎县| 建宁县| 东阿县| 吕梁市| 高尔夫| 新津县| 全州县| 安康市| 泗水县| 玉田县| 遵义县| 信丰县| 抚顺县| 德格县| 灵璧县| 弥渡县| 彭泽县| 马尔康县| 静宁县| 宜城市| 喜德县| 晋宁县| 黔江区| 横山县| 静海县| 静乐县| 逊克县| 双峰县| 巢湖市| 平利县| 陆川县| 宿迁市| 佛坪县| 公安县|