Android: Detecting USB
Is there any way to know(programmatically) in your Activity/App开发者_StackOverflow社区lication that the user has connected your phone to PC through USB?
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
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);
This works for me.
Add this on your 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>
And Create your 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();
}
}
}
}
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;
}
}
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")) {...}
}
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
精彩评论