软件崩溃重启
一、捕获全局异常
步骤1 、创建自定义的异常处理器
public class SelfUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Context mContext;
public SelfUncaughtExceptionHandler(Context context) {
this.mContext = context;
}
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
Log.d("测试", "成功捕捉到异常 " + e.toString());
// 捕捉到异常后,后续执行步骤
}
}
步骤2、 在Application类中设置自定义的异常处理器
将MyApplication类设置为应用程序的入口点
<application
android:name=".MyApplication"
...>
...
</application>
设置为默认的异常处理器
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Context context = this.getApplicationContext();
// 第三方库
if (ProcessPhoenix.isPhoenixProcess(context)){
return;
}
// 创建自定义的异常处理器对象
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = new SelfUncaughtExceptionHandler(context);
// 设置自定义的异常处理器为默认的异常处理器
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
}
}
二、软件重启
1、方法1: AlarmManager(高版本不适用,会显示系统Launcher桌面)
private void setAlarmManager(){
Intent intent = new Intent();
intent.setClass(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis()+100, pendingIntent);
Process.killProcess(Process.myPid());
System.exit(0);
}
2、方法2:直接启动Activity(必须是Standard模式)
public class SelfUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Context mContext;
public SelfUncaughtExceptionHandler(Context context) {
this.mContext = context;
}
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
Log.d("测试", "成功捕捉到异常 " + e.toString());
// 使用第三方库重启
restartApp();
}
// 方法二
private void restartApp() {
Intent intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
// Process应该为android.os中的
Process.killProcess(Process.myUid());
System.exit(0);
}
}
3、方法3:ProcessPhoenix(第三方库)
1、导入该库(见完整代码部分)
2 、使用(mContext为MyApplication的context)
// 方式一:进入默认Activity
ProcessPhoenix.triggerRebirth(mContext);
// 方式二:进入指定Activity
Intent intent = new Intent(//指定的Activity//);
ProcessPhoenix.triggerRebirth(mContext,intent);
3、原理
ProcessPhoenix的原理:
(1)、当调用triggerRebirth方法的时候,会启动一个透明的Activity,这个Activity运行在:phoenix进程
(2)、Activity启动后,杀掉主进程,然后用:phoenix进程拉起主进程的Activity
(3)、关闭当前Activity,杀掉:phoenix进程
三、完整代码(以软件重启方法3为例)
引入ProcessPhoenix库
implementation 'com.jakewharton:process-phoenix:2.1.2'
新建SelfUncaughtExceptionHandler类
public class SelfUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Context mContext;
public SelfUncaughtExceptionHandler(Context context) {
this.mContext = context;
}
@Override
public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
Log.d("测试", "成功捕捉到异常 " + e.toString());
// 使用第三方库重启
ProcessPhoenix.triggerRebirth(mContext);
}
}
新建MyApplication类
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Context context = this.getApplicationContext();
// 第三方库
if (ProcessPhoenix.isPhoenixProcess(context)) {
return;
}
// 创建自定义的异常处理器对象
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = new SelfUncaughtExceptionHandler(context);
// 设置自定义的异常处理器为默认的异常处理器
Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
}
}
将MyApplication类设置为应用程序的入口点
<application
android:name=".MyApplication"
...>
...
</application>