我的应用程序运行的服务会在设备重启或重新安装(更新)时终止。我添加了两个广播接收器来捕获这些事件-BOOT_COMPLETED和ACTION_MY_PACKAGE_REPLACED。
ACTION_MY_PACKAGE_REPLACED接收器似乎不起作用。这是我所拥有的:
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上进行测试。通过检查我的服务是否在“设置/应用程序”中运行来测试成功,该服务在重新启动时会执行,但不会重新安装。
我考虑了以下内容,即在Android Studio 1.0+中,清单合并意味着我无法将两个接收者合并为一个类。请参阅未收到ACTION_MY_PACKAGE_REPLACED,并且对于名称相同但内容不同的接收者,Android清单合并失败
您可能已经知道了这一点,但是清单中的操作名称是错误的,而不是:
android.intent.action.ACTION_MY_PACKAGE_REPLACED
它应该是
android.intent.action.MY_PACKAGE_REPLACED
您也可以adb shell
出于测试目的手动触发接收器:
adb shell am broadcast -a android.intent.action.MY_PACKAGE_REPLACED -n com.example.myapp/.ReInstallReceiver
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句