简体   繁体   中英

How to get sticky broadcast data when BroadcastReceiver is enabled?

I have a BroadcastReceiver that is registered on the manifest and it is initially disabled android:enabled="false" .

Manifest:

<receiver
            android:name="com.sample.name.MainWidgetBroadcastReceiver"
            android:enabled="false"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.sample.name.intent.action.METADATA_UPDATE" />
            </intent-filter>
        </receiver>

The intent I receive com.sample.name.intent.action.METADATA_UPDATE is sticky. How can I get this sticky data when I enable the receiver? It would be fine if the solution triggers the BroadcastReceiver or if I have to pull it my self. I would prefer not having to register the broadcast just to get this data...

PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(
                new ComponentName(context, MainWidgetBroadcastReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

Oops! I guess I should read the APIs harder... registerReceiver(null, intentFilter) would do it!

If you know the Intent your are registering for is sticky, you can supply null for your receiver. In this case, no receiver is registered -- the function simply returns the sticky Intent that matches filter. In the case of multiple matches, the same rules as described above apply.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM