user3290180
user3290180

Reputation: 4410

Android: How to make a single Receiver and switch between intents?

I have two receivers

    <receiver
        android:name=".BootReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name=".DateReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
    </receiver>

This are their java implementations

public class BootReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Log.i("BootReceiver", "ok");
   }
}

public class DateReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
     Log.i("DateReceiver", "new date");
   }
}

I'd like to make a single receiver with all intent-filters and make a switch when it receives an intent.

Upvotes: 0

Views: 370

Answers (2)

Jivraj S Shekhawat
Jivraj S Shekhawat

Reputation: 352

Add it in manifest like this

 <receiver android:name=".EventHandler" >
            <intent-filter>
                <action android:name="android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT" />
                <action android:name="android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED" />
                <action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED" />
                <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
                <action android:name="android.net.wifi.STATE_CHANGE" />
                <action android:name="android.intent.action.HEADSET_PLUG"/>
                <action android:name="android.hardware.action.NEW_PICTURE" />
                <action android:name="android.hardware.action.NEW_VIDEO" />
                <action android:name="android.intent.action.SCREEN_OFF" />
                <action android:name="android.intent.action.SCREEN_ON" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

and receive them something like that....

@Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "---------------------------------------");
        if(intent.getAction().equalsIgnoreCase(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
            // Do Some Thing here...
        }
}

Upvotes: 1

neuo
neuo

Reputation: 733

    <receiver
        android:name=".DataAndBootReceiver"
        android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED" />
        </intent-filter>
    </receiver>



    public class DataAndBootReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction()
                    .equals("android.intent.action.DATE_CHANGED")) {
                Log.i("Receiver", "Data");
            } else {
                Log.i("Receiver", "Boot");
            }
        }
    }

Upvotes: 2

Related Questions