我正在尝试使用广播接收器来让我的设备连接到 USB 或车载底座,但没有得到正确的结果。请帮忙?提前致谢。收货人代码为:
public class CarDockReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Car Dock Receiver registerd", Toast.LENGTH_SHORT).show();
switch (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)) {
case BatteryManager.BATTERY_PLUGGED_AC:
Toast.makeText(context, "Battery plugged AC", Toast.LENGTH_SHORT).show();
break;
case BatteryManager.BATTERY_PLUGGED_USB:
Toast.makeText(context, "Battery plugged USB", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
清单文件中的接收者是:
<receiver
android:name=".CarDockReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>