是否有用户通过USB将手机连接到PC的任何办法知道(编程)在活动/应用程序?
有些人使用被弃用的最新版本的Android UMS_CONNECTED
的另一个问题是,它不工作与MTP功能的设备建议
其他人则建议使用BatteryManager
的,更确切地说ACTION_BATTERY_CHANGED
以及BATTERY_PLUGGED_AC
和BATTERY_PLUGGED_USB
这是完美的,如果你要检测的电池或移动设备的充电状态,但不是USB连接的一个非常好的指标。使用电池管理器易于在较旧的Android片剂如XOOM,所述ICONIA标签A510,和旧华硕片剂故障。
为了纯粹的检测设备被插入电脑,你可以:在地方android.hardware.usb.action.USB_STATE
东西使用connected
和BatteryManager
代码示例
public static boolean isConnected(Context context) {
intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
return intent.getExtras().getBoolean("connected");
}
希望这可以帮助
之所以能够通过以下注册广播接收器检测到USB连接,
IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED); BroadcastReceiver bd = new intentReceiver(); registerReceiver(bd, mIntentFilter);
的Manifest.xml:
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.ums_connected" />
</intent-filter>
</receiver>
MyReceiver:
public class MyReceiver extends BroadcastReceiver{
if (intent.getAction().equalsIgnoreCase(
"android.intent.action.UMS_CONNECTED")) {...}
}
如果你想要做的是检测是否有访问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;
}
}
这对我的作品。
收藏此对您的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>
并创建BroadcastReceiver
。
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();
}
}
}
}
同时检查android.intent.action.ums_connected
的主要问题是,使用MTP协议(如三星Nexus银河)设备不接收该广播。
这就是为什么我使用的是另一种方式,当智能手机插入或拔出来检测:
我检查batery状态。还有,当一个事件发生对电池称为意图称为ACTION_BATTERY_CHANGED
。在这种意图有含一些信息,一些额外的字段。其中之一是EXTRA_PLUGGED
:
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.
其他常数BATTERY_PLUGGED_AC
和BATTERY_PLUGGED_USB
因此,与此广播,你可以知道,如果智能手机已在USB被插入,即使它使用MTP协议。
要知道,如果智能手机被拔掉你探微必须检查时,从EXTRA_PLUGGED
的BATTERY_PLUGGED_USB
值更改为0