可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using ACTION_MY_PACKAGE_REPLACED to receive when my app is updated or resinstalled. My problem is that the event is never triggered (I tried Eclipse and real device). This is what I do:
Manifest:
Code:
public class MyEventReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if ("android.intent.action.ACTION_MY_PACKAGE_REPLACED".equals(intent.getAction())) { //Restart services } } }
This code is simple, in real one I used other events like BOOT_COMPLETED and others, and they work but ACTION_MY_PACKAGE_REPLACED. Thanks.
回答1:
for some reason, a google developer (named "Dianne Hackborn") thinks it is possible to register to the PACKAGE_REPLACED intent of the current app alone (link here).
however, i can't find any way of doing it correctly, so i've made a compromise: it will use the newest API when available.
Sadly, I can't find out why it can't be debugged, but it does work (you can write to the log if you wish).
here's the code:
manifest:
res/values/version_checks.xml
- false
- true
res/values-v12/version_checks.xml
- true
- false
OnUpgradeBroadcastReceiver.java
public class OnUpgradeBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { if (VERSION.SDK_INT
回答2:
The accepted answer doesn't work any more with Android Studio 1.0+ because of manifest merge issues, as seen here. Totally based on android developer's answer, I fixed the issue with the following implementation:
AndroidManifest.xml:
/res/values/resources.xml:
truefalse
/res/values-v12/resources.xml:
falsetrue
UpdateReceiver.java:
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class UpdateReceiver extends BroadcastReceiver { public static class LegacyUpdateReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent != null && intent.getData() != null && context.getPackageName().equals(intent.getData().getSchemeSpecificPart())) { onUpdate(context); } } } @Override public void onReceive(Context context, Intent intent) { onUpdate(context); } public static void onUpdate(Context context) { Log.d("LOG", "Current app updated"); } }
回答3:
Getting information from all the users I could solve my situation this way. All of them were right, with little points to notice:
In manifest:
And code:
public class MyEventReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) { if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName())) { //Restart services. } } } }
In my Android release (2.3 Gingerbread) I was not able to use MY_PACKAGE_REPLACED but we solved using PACKAGE_REPLACED (will advise of any app been replaced) but asking if it is ours with:
if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName())) { }
Thanks to all
回答4:
I just want to add my own two pence worth here, because I had problems getting this to work and debugging it .. I am using a Lollipop device:
This is the code I used:
To debug it, you need to make sure you have installed the update on your phone, just via Eclipse debug is fine, and opened the app at least one time, then you can simply edit your debug configuration:
eclipse > run > debug configurations > launch action (do nothing) > then F11 like normal
I confirmed it worked by writing a small file to the SD card
回答5:
Are you trying it on a API>=12 device/emulator? This broadcast will not be sent on prior cases as it is API 12. If you need your app to receive this for Pre-ICS and the old honey comb devices,
try:
if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) { if (!intent.getData().getSchemeSpecificPart() .equals(context.getPackageName())) { return; } }
回答6:
Simple Manifest working in all version :
回答7:
You need to add data scheme to the intent filter like below