我也相信这是重复的问题。您是否尝试过MY_PACKAGE_REPLACED
在意图过滤器中使用注册广播?(设备重启时也会触发,可能有用)
显现:
<receiver
android:name=".OnUpdateReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
代码:
public class OnUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null ||
(!Intent.ACTION_MY_PACKAGE_REPLACED.equals(intent.getAction()) &&
!Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())))
return;
// your code, start your Service again or
// Activity if it is REALLY desired behavior
}
}
您可以使用此命令测试此代码,无需“真正”更新
adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.yourapp/.OnUpdateReceiver
还支持直接来自官方 Google Play Core API 的应用内更新,也许这可以用最少的代码解决您的问题