用代码实现断开Android手机USB连接
用代码 实现了一个小功能:断开android手机USB连接。
<span style="white-space:pre"> </span>// 尝试断开USB连接
boolean disconnectUSB() {
String state = "none";// 禁用所有usb
//String state = "adb";// 仅使用adb
//String state = "mtp,adb";// 使用mtp与adb
SystemProperties.set("sys.usb.config", state);
// wait for the transition to complete.
// give up after 1 second.
for (int i = 0; i < 20; i++) {
// State transition is done when sys.usb.state is set to the new configuration
if (state.equals(SystemProperties.get("sys.usb.state"))) return true;
SystemClock.sleep(50);
}
return false;
}
也可通过反射机制进行调用,但其实最终实际执行的还是上面的代码。