Android: Detecting USB

2019-01-06 17:51发布

问题:

Is there any way to know(programmatically) in your Activity/Application that the user has connected your phone to PC through USB?

回答1:

Some people suggested using UMS_CONNECTED which is deprecated as of recent version of Android The other problem with that is that it does not work with MTP enabled devices

Others suggested the use of the BatteryManager, more precisely ACTION_BATTERY_CHANGED as well as BATTERY_PLUGGED_AC and BATTERY_PLUGGED_USB This is perfect if you want to detect the Battery or Charging status of the device, but is not a really good indicator of a USB connection. Using the battery manager is prone to failure on older android tablets such as the XOOM, the ICONIA tab A510, and the older Asus tablets.

To purely detect that the device was plugged on a PC you can: Use android.hardware.usb.action.USB_STATE and connected in place of the BatteryManager stuff

Code sample

public static boolean isConnected(Context context) {
        intent = context.registerReceiver(null, new IntentFilter("android.hardware.usb.action.USB_STATE"));
        return intent.getExtras().getBoolean("connected");
    }

Hope this helps



回答2:

Was able to detect USB connection by registering a broadcast receiver by following,

IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_UMS_CONNECTED);

BroadcastReceiver bd = new intentReceiver();
registerReceiver(bd, mIntentFilter);



回答3:

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")) {...}
}


回答4:

If all you want to do is detect whether you have access to the sdcard, then the following will work:

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


回答5:

The main problem while checking android.intent.action.ums_connected is that devices using the MTP protocol (such as the Samsung Nexus Galaxy) don't receive this broadcast.

This is why I'm using another way to detect when the Smartphone is plugged or unplugged:

I check the batery state. There is an intent called ACTION_BATTERY_CHANGED called when an event happens on the battery. In this intent there are some extra fields containing some informations. One of them is 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.

The other constants are BATTERY_PLUGGED_AC and BATTERY_PLUGGED_USB

So with this broadcast you can know if the Smartphone has been plugged in USB even if it uses the MTP protocol.

To know if the Smartphone is unplugged you juste have to check when the EXTRA_PLUGGED value changes from BATTERY_PLUGGED_USB to 0



标签: android usb