gpt4 book ai didi

android - 检查 USB 连接或不在按钮中单击 android

转载 作者:行者123 更新时间:2023-11-30 03:11:37 24 4
gpt4 key购买 nike

单击按钮,我想检查是否有任何 USB 连接到设备。如果已连接,则执行一些操作(可能是 toast )。单击按钮时会广播检查 USB 是否已连接。

最佳答案

我不确定所有的挑剔,但这里是您测试设备是否正在充电的方法:

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;
}
}

发现于 this问题。

编辑:使用UsbManager.getDeviceList你得到所有连接的设备。因此,如果大小为 0,则未连接 USB。

关于android - 检查 USB 连接或不在按钮中单击 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20855874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com