Android 安卓:检测USB
在您的活动/应用程序中是否有任何方法(以编程方式)知道用户已通过USB将您的手机连接到PC?可以通过以下方式注册广播接收器来检测USB连接: IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED); BroadcastReceiver bd = new intentReceiver(); registerReceiver(bd, mIntentFilter); IntentFilter minentfilter=新的IntentFilter(Intent.ACTION\u UMS\u已连接); BroadcastReceiver bd=新的intentReceiver(); 寄存器接收器(bd、MinentFilter); Manifest.xml:Android 安卓:检测USB,android,usb,Android,Usb,在您的活动/应用程序中是否有任何方法(以编程方式)知道用户已通过USB将您的手机连接到PC?可以通过以下方式注册广播接收器来检测USB连接: IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED); BroadcastReceiver bd = new intentReceiver(); registerReceiver(bd, mIntentFilter); IntentFilter minentf
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.ums_connected" />
</intent-filter>
</receiver>
如果您只想检测您是否有权访问SD卡,则以下操作将起作用:
private boolean canWriteToFlash() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Read only isn't good enough
return false;
} else {
return false;
}
}
检查
android.intent.action.ums_connected
时的主要问题是,使用MTP协议的设备(如三星Nexus Galaxy)无法接收此广播
这就是为什么我使用另一种方法来检测智能手机何时被插入或拔出:
我检查电池状态。当电池上发生事件时,会调用一个名为ACTION\u BATTERY\u CHANGED
的意图。在这个意图中,有一些额外的字段包含一些信息。其中一个是额外插入的:
Indicating whether the device is plugged in to a power source; 0 means it is on battery, other constants are different types of power sources.
其他常数为电池插入式\u AC
和电池插入式\u USB
因此,通过此广播,您可以知道智能手机是否已插入USB,即使它使用MTP协议
要知道智能手机是否已拔出插头,只需检查EXTRA\u-pluging
值何时从BATTERY\u-pluging\u-USB
更改为0
一些人建议使用UMS\u-CONNECTED
,这在最新版本的Android中已被弃用
另一个问题是,它不适用于启用MTP的设备
其他人建议使用电池管理器
,更准确地说是电池更换动作
,以及电池插电(AC
和电池插电)USB
如果您想检测设备的电池或充电状态,但这并不是USB连接的一个很好的指示器,那么这是完美的。
在旧的android平板电脑上使用电池管理器容易出现故障,如XOOM、ICONIA tab A510和旧的华硕平板电脑
要纯粹检测设备是否已插入PC,您可以:
使用android.hardware.usb.action.usb\u状态
和已连接
代替电池管理器
代码示例
public static boolean isConnected(Context context) {
intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
return intent.getExtras().getBoolean("connected");
}
希望这对我有帮助
将此添加到您的AndroidManifest.xml中
<receiver android:name=".PlugInControlReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
如果设备使用的是MTP协议而不是UMS协议(例如Galaxy Nexus使用的是MTP协议),它就不起作用了@darkheir感谢您指出这一点。一旦我找到一个通用的解决方案,就会更新答案。@Fresh_Meat在下面的主题中,我建议一个适用于MTP和UMS的解决方案:其目的是检查电池状态,同时检查智能手机是否通过USB或USB连接AC@Fresh_Meat真是太好了:-)这对XOOM这样的平板电脑不起作用,其中电源和usb是两个不同的端口。从文档中可以看出这实际上检测到了什么。在我的例子中,我试图诊断一根有故障的USB电缆,有时电源状态没有改变,但USB断开或断开-后者是我试图检测的。这个MyReceiver代码甚至没有意义。。类定义中的if?
<receiver android:name=".PlugInControlReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.hardware.usb.action.USB_STATE" />
</intent-filter>
</receiver>
public class PlugInControlReceiver extends BroadcastReceiver {
@Override public void onReceive(final Context context, Intent intent) {
String action = intent.getAction();
Log.v("PlugInControlReceiver","action: "+action);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if(action.equals("android.hardware.usb.action.USB_STATE")) {
if(intent.getExtras().getBoolean("connected")){
Toast.makeText(context, "USB Connected", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "USB Disconnected", Toast.LENGTH_SHORT).show();
}
}
} else {
if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(context, "USB Connected", Toast.LENGTH_SHORT).show();
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(context, "USB Disconnected", Toast.LENGTH_SHORT).show();
}
}
}
}