在Android开发的过程中,我们有时会需要重启我们的App,比如当我们的App出现了异常崩溃时,用户退出App后再次打开App可以自动重启,或者当我们的App需要重新初始化时,也需要重启。本文将介绍Android开发中如何重启App的方法以及其原理。
### 通过PackageManager获取Intent重启应用
通过PackageManager我们可以获取到当前应用的Intent,利用重启的Intent将当前应用重新启动。
我们需要创建一个新的Intent,并将FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK标记设置为Intent的标准行为,已达到将整个应用程序关闭的效果。同时,我们需要传递给系统重启应用的Package name以及对应的Activity的Launch Activity。
下面是示例代码:
```java
Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
```
### 通过PendingIntent重启应用
第二种方法是使用PendingIntent,通常用于应用崩溃后应用自动重启,不需要用户操作。
我们需要在Application类中注册一个Thread.UncaughtExceptionHandler,这个Handler用于捕获应用崩溃异常时的回调。在回调中,我们使用一个Intent,设置FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK,然后使用PendingIntent获取该Intent的PendingIntent,最后把PendingIntent传递给系统的AlarmManager重启应用。
下面是示例代码:
1. 注册Thread.UncaughtExceptionHandler
```java
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
}
private class MyExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
System.exit(0);
}
}
}
```
在uncaughtException()方法中,我们创建了一个将要启动应用的Intent,将其设置为FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK,这样将整个应用程序关闭。
在完成Intent的设置之后,我们还创建了一个PendingIntent,将PendingIntent传递给AlarmManager,这样当应用崩溃时,5秒后将会启动该PendingIntent所关联的Activity。
2. 设置MainActivity不保留活性
在AndroidManifest.xml中,将MainActivity设置不保留活性:
```xml
android:name=".MainActivity" android:configChanges="orientation|screenSize" android:excludeFromRecents="true" android:launchMode="singleTask" android:noHistory="true" android:stateNotNeeded="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" />
```
在这里,我们需要设置android:noHistory="true" 和 android:excludeFromRecents="true",这样在系统的“最近任务列表”中,将不再保留MainActivity的活性,保证该Activity在应用崩溃后正常被销毁以达到“整个应用程序关闭”的效果。
### 常见错误
1. 解决重启应用的应用崩溃问题
由于我们的应用崩溃时,有一定的机率可能是由于Activity中的某些元素引起的,导致了应用崩溃。如果重启应用时,自动进入了该问题Activity,那么应用将会再次崩溃。
因此,我们在实现自动重启应用时,需要检查崩溃后的状态,如果是在某个Activity中崩溃的,就不应自动重启该Activity,而是应该自动跳转到应用的主Activity。可以在上面提到的Thread.UncaughtExceptionHandler中加入解决该问题Activity后跳转至主Activity的代码,如下所示:
```java
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pendingIntent);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
```
这里使用了android.os.Process.killProcess(android.os.Process.myPid())来杀死当前进程。
2. Android O以上版本的限制
在Android O(8.0)及以上版本中,Android限制了后台应用的行为,仅支持启动前台服务,并且在启动服务前会引发用户确认弹窗以确保用户同意该服务。
因此,在Android O及以上版本中,我们无法使用上述两种方法来实现自动重启应用。如果我们的应用崩溃时需要自动重启,需要将我们的应用设置为前台服务,即在Application.onCreate()方法中开启一个前台服务,并设置为系统Notification的内容来让用户知悉。
### 总结
本文介绍了重启App的两种方法,并解决了一些常见错误。当我们需要使用重启应用这一功能时,可以根据自己的需求选择不同的方法进行实现。除了以上提到的问题,我们在实际开发中还需考虑更多的细节问题,比如如何保存用户的状态等,这些问题需要进一步的研究和调整。