Reputation: 4410
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
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
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