1. Directly upload the code. When your app installs a new version, the system will send out 3 broadcasts. We only need to register the broadcast in the Android program, and specify to receive the (android.intent.action.PACKAGE_REPLACED) broadcast after the update application is completed. After receiving the broadcast, you can restart the app
/*android.intent.action.PACKAGE_REMOVED 卸载应用完成后收到
android.intent.action.PACKAGE_ADDED 安装应用完成后收到
android.intent.action.PACKAGE_REPLACED 更新应用完成后收到,
在此之前后先收到前两个广播(PACKAGE_REMOVED 然后是 PACKAGE_ADDED 最后是 PACKAGE_REPLACED )*/
class UninstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
//这里做重新启动app
ActivityManager manager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
manager.restartPackage("com.example.test");
}
}
2. Static registration broadcast, you can also choose code registration broadcast
//在AndroidManifest.xml中定义如下
<receiver android:name=".UninstallReceiver">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_PACKAGE_ADDED" />
<action android:name="android.intent.action.ACTION_PACKAGE_REPLACED" />
<action android:name="android.intent.action.ACTION_PACKAGE_REMOVED" />
</intent-filter>
</receiver>
3. Code registration broadcast
//注册广播
IntentFilter intentFilter = new IntentFilter();
UninstallReceiver uninstallReceiver = new UninstallReceiver();
intentFilter.addAction(Intent.SCREEN_ON);
intentFilter.addAction(Intent.BOOT_COMPLETED);
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
intentFilter.addDataScheme("package");
registerReceiver(uninstallReceiver, intentFilter);
4. You can also refer to this article
https://blog.csdn.net/u012346890/article/details/111516870