Skip to content Skip to sidebar Skip to footer

Detect Usb Connection And Check If It Is A Pc

I want to detect wheather my android phone is connected via USB or not...for this i´m using this code: public static boolean isConnected(Context context) { intent = context.re

Solution 1:

You can use this IntentFilter :

IntentFilterifilter=newIntentFilter(Intent.ACTION_BATTERY_CHANGED);
IntentbatteryStatus= context.registerReceiver(null, ifilter);

// Are we charging / charged?intstatus= batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
booleanisCharging= status == BatteryManager.BATTERY_STATUS_CHARGING ||
                 status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?intchargePlug= batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
booleanusbCharge= chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
booleanacCharge= chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

If isCharging and usbCharge booleans are set to true, you are connected to a PC via USB.

Hope this helps.

Found on http://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Post a Comment for "Detect Usb Connection And Check If It Is A Pc"