Search code examples
androidandroid-studiobroadcastreceiverandroid-broadcastreceiver

Multiple broadcast Actions in single receiver not working After adding <data android:scheme="package" /> with PACKAGE_ADDED and PACKAGE_REMOVED


I am creating an application which will send me events of various actions, I have added bellow events in the single class which is working perfectly.

<receiver android:name=".activity.SettingsEventReceiver" >
    <intent-filter>
        <!--*************Bluetooth*********************-->
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />

        <!--*************Hotspot*********************-->
        <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />

        <!--*************AirplaneMode*********************-->
        <action android:name="android.intent.action.AIRPLANE_MODE"/>

        <!--*************VolumeChange*********************-->
        <action android:name="android.media.VOLUME_CHANGED_ACTION" />

        <!--*************ChargingSettings*********************-->
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />

        <!--*************Sim status changed Event*********************-->
        <action android:name="android.intent.action.SIM_STATE_CHANGED" />

        <!--*************Reboot Event*********************-->
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>

    </intent-filter>
</receiver>

but whenever I do add bellow actions, I dont receive a single event

<!--*************Install/Uninstall Event*********************-->
    <action android:name="android.intent.action.PACKAGE_ADDED"/>
    <action android:name="android.intent.action.PACKAGE_REMOVED"/>
    <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH"/>
    <data android:scheme="package" />

How can I overcome this issue? Please help


Solution

  • When you add the <data> element to the <intent-filter> you will only get broadcast events that match that <data> element. Most of the broadcast Intents you've listed do not contain any data.

    You can specify multiple <intent-filter>, like this:

    <receiver android:name=".activity.SettingsEventReceiver" >
        <intent-filter>
            <!--*************Bluetooth*********************-->
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
    
            <!--*************Hotspot*********************-->
            <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
    
            <!--*************AirplaneMode*********************-->
            <action android:name="android.intent.action.AIRPLANE_MODE"/>
    
            <!--*************VolumeChange*********************-->
            <action android:name="android.media.VOLUME_CHANGED_ACTION" />
    
            <!--*************ChargingSettings*********************-->
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    
            <!--*************Sim status changed Event*********************-->
            <action android:name="android.intent.action.SIM_STATE_CHANGED" />
    
            <!--*************Reboot Event*********************-->
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
    
        </intent-filter>
    
        <intent-filter>
            <!--*************Install/Uninstall Event*********************-->
            <action android:name="android.intent.action.PACKAGE_ADDED"/>
            <action android:name="android.intent.action.PACKAGE_REMOVED"/>
            <action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH"/>
            <data android:scheme="package" />
        </intent-filter>
    </receiver>