Android:广播 ACTION_MY_PACKAGE_REPLACED 从未收到 [英] Android: Broadcast ACTION_MY_PACKAGE_REPLACED never received
问题描述
我的应用运行的服务在设备重启或应用重新安装(更新)时终止.我添加了两个广播接收器来捕捉这些事件 - BOOT_COMPLETED 和 ACTION_MY_PACKAGE_REPLACED.
My app runs a service which is terminated when the device reboots or the app is reinstalled (updated). I've added two broadcast receivers to catch those events - BOOT_COMPLETED and ACTION_MY_PACKAGE_REPLACED.
ACTION_MY_PACKAGE_REPLACED 接收器似乎不起作用.这是我所拥有的:
The ACTION_MY_PACKAGE_REPLACED receiver just doesn't seem to work. Here's what I have:
AndroidManifest.xml:
AndroidManifest.xml:
<receiver android:name=".RebootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name=".ReInstallReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_MY_PACKAGE_REPLACED"/>
</intent-filter>
</receiver>
重启接收器:
public class RebootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Logg.d("Reboot completed. Restarting service");
context.startService(new Intent(context, MyService.class));
}
}
重新安装接收器:
public class ReInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Logg.d("App Upgraded or Reinstalled. Restarting service");
context.startService(new Intent(context, MyService.class));
}
}
运行minSdk=16;在运行 KitKat 的 Galaxy S3 上进行测试.通过检查我的服务是否在设置/应用程序中运行来测试成功,它在重新启动时运行,但不重新安装.
Running minSdk=16; Testing on Galaxy S3 running KitKat. Testing success by checking if my service is running in Settings/Applications, which it does on reboot, but not reinstall.
我已经考虑了以下注释,其中说在 Android Studio 1.0+ 中,清单合并意味着我不能将两个接收器合并为一个类.请参阅未收到ACTION_MY_PACKAGE_REPLACED和Android 清单合并失败对于名称相同但内容不同的接收者
I've taken into account notes from the following, which say that in Android Studio 1.0+, manifest mergers mean I can't combine two receivers into one class. See ACTION_MY_PACKAGE_REPLACED not received and Android manifest merger fails for receivers with same name but different content
推荐答案
您可能已经想到了这一点,但是您在清单中的操作名称是错误的,而不是:
You probably figured this out already, but your action name in the manifest is wrong, instead of:
android.intent.action.ACTION_MY_PACKAGE_REPLACED
应该是
android.intent.action.MY_PACKAGE_REPLACED
您也可以使用 adb shell
手动触发接收器以进行测试:
You can also manually trigger the receiver using adb shell
for testing purposes:
adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.example.myapp/.ReInstallReceiver
这篇关于Android:广播 ACTION_MY_PACKAGE_REPLACED 从未收到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!