我有两个接收器
<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>
这是他们的java实现
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");
}
}
我想制作一个包含所有intent-filters
的接收器,并在收到意图时进行切换。
答案 0 :(得分:2)
<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");
}
}
}
答案 1 :(得分:1)
将其添加到清单中
<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>
并接受类似的东西......
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "---------------------------------------");
if(intent.getAction().equalsIgnoreCase(Intent.ACTION_AIRPLANE_MODE_CHANGED)){
// Do Some Thing here...
}
}