相关疑难解决方法(0)

Android:广播ACTION_MY_PACKAGE_REPLACED从未收到过

我的应用程序运行的服务在设备重新启动或重新安装(更新)应用程序时终止.我添加了两个广播接收器来捕获这些事件 - 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>
Run Code Online (Sandbox Code Playgroud)

RebootReceiver:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

ReInstallReceiver:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

运行minSdk = 16; 在运行KitKat的Galaxy S3上进行测试.通过检查我的服务是否在"设置/应用程序"中运行来测试成功,它在重新启动时执行,但不重新安装.

我已经考虑了以下注释,其中说在Android Studio 1.0+中,清单合并意味着我无法将两个接收器合并为一个类.请参阅ACTION_MY_PACKAGE_REPLACED未收到,并且对于具有相同名称但内容不同的接收者,Android清单合并失败

android broadcastreceiver android-broadcast android-studio

6
推荐指数
3
解决办法
7256
查看次数