在按钮中检查是否已连接USB单击android
单击按钮,我想检查是否有USB连接到设备。如果已连接,则会执行一些操作(可能是祝酒)。单击按钮时,会播放一段广播,以检查USB是否已连接。我不确定是否存在所有的批评,但下面是如何测试设备是否正在充电:在按钮中检查是否已连接USB单击android,android,usb,broadcasting,Android,Usb,Broadcasting,单击按钮,我想检查是否有USB连接到设备。如果已连接,则会执行一些操作(可能是祝酒)。单击按钮时,会播放一段广播,以检查USB是否已连接。我不确定是否存在所有的批评,但下面是如何测试设备是否正在充电: public class PowerUtil { public static boolean isConnected(Context context) { Intent intent = context.registerReceiver(null, new IntentFi
public class PowerUtil {
public static boolean isConnected(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
}
在问题中发现
编辑:使用,您可以获得所有连接的设备。因此,如果大小为0,则不会连接USB。@Jerry请参阅我的编辑。使用达尔梅什答案中的提示找到它