java - Detect USB Connection and check if it is a PC -


i want detect wheather android phone connected via usb or not...for i´m using code:

public static boolean isconnected(context context) {     intent = context.registerreceiver(null, new intentfilter("android.hardware.usb.action.usb_state"));     return intent.getextras().getboolean("connected"); } 

but there possibility explicit know if connected pc?! because solution have fires event connect phone printer or sth else usb port...

i tried solution @nios posted, if ischarging , usbcharge booleans set true, it´s not guaranted connected pc...even if connected printer both booleans bet set true...thats problem

you can use intentfilter :

intentfilter ifilter = new intentfilter(intent.action_battery_changed); intent batterystatus = context.registerreceiver(null, ifilter);  // charging / charged? int status = batterystatus.getintextra(batterymanager.extra_status, -1); boolean ischarging = status == batterymanager.battery_status_charging ||                  status == batterymanager.battery_status_full;  // how charging? int chargeplug = batterystatus.getintextra(batterymanager.extra_plugged, -1); boolean usbcharge = chargeplug == batterymanager.battery_plugged_usb; boolean accharge = chargeplug == batterymanager.battery_plugged_ac; 

if ischarging , usbcharge booleans set true, connected pc via usb.

hope helps.

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


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -