首页 > 解决方案 > 自定义意图广播

问题描述

Android 8.0 (Oreo) 引入了新的电池使用优化,包括隐式广播。我们有主应用程序和额外安装的插件 (apks)。主应用程序发送带有动作“POLL_PLUGINS”的自定义广播,以确定已安装哪些并与它们共享授权数据。问题是在 android 8.0 上的插件中无法接收广播。这是发送广播的代码:

Intent intent=new Intent();
intent.setAction("<PACKAGE_NAME>.POLL_PLUGINS");
intent.putExtra ("auth", "<ENCRYPTED_DATA>"));
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);

和插件的主要接收器:

<receiver
        android:name=".PluginReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter android:priority="100000000">
            <action android:name="<PACKAGE_NAME>.POLL_PLUGINS"/>
            <action android:name="<PACKAGE_NAME>.plugin1.START"/>
        </intent-filter>
    </receiver>

和插件的接收器:

public class PluginReceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d ("Plugin/Receiver","Plugin1 received data"); //<-- not being executed at all
        MainConn.parseBroadCastIntent (context,intent,R.mipmap.ico_crm, MainActivity.class);
    }
}

在以前的机器人上工作得很好。我如何使用相同的方案或是否有其他解决方法?

标签: javaandroidandroid-8.0-oreo

解决方案


注意:如果您的应用以 API 级别 26 或更高级别为目标,则您不能使用清单为隐式广播(不专门针对您的应用的广播)声明接收器,但一些不受该限制的隐式广播除外。在大多数情况下,您可以改用计划作业。

更多详情见详情


推荐阅读