简体   繁体   中英

How to restrict user to connect USB when my Android app is running?

I want the user to stop the USB connection to the PC when my app is running on his Android mobile like Google Pay and some other apps do. How to do this as. How to show the user an alert to remove the USB connection when he connects mobile to PC through USB when my app is running on that mobile?

A1: You said "users often connect their devices to their computers to add music into the apps folder", I guess you mean that the Sd card has connected to PC in MassStorage mode, you can check this as following:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_SHARED.equals(state)) {
    // Sd card has connected to PC in MSC mode
}

A2: You said "This won't work if the user starts the app after connecting to USB", I can't agree with you, in ICS, you can register a receiver to listen android.hardware.action.USB_STATE changes, the intent broadcasted by system is in mode, that to say even the receiver in your app is registered after usb cable connecting, you can also get this message: 模式,也就是说即使你的应用程序中的接收器在usb电缆连接后注册,你也可以得到这个消息:

Intent intent = context.registerReceiver(...);
if (intent != null) {
    boolean isConnected = intent.getBooleanExtra(USB_CONNECTED, false);
    boolean isMscConnection = intent.getBooleanExtra(USB_FUNCTION_MASS_STORAGE, false);
}

the intent returned in the method mentioned above the message you missed before usb cable connection to PC.

refer to the link below for details: http://www.androidadb.com/source/toolib-read-only/frameworks/base/core/java/android/hardware/usb/UsbManager.java.html

or you can use a intentfilder to check battery status

You can use this IntentFilter:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

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

// How are we 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 and usbCharge booleans are set to true, you are connected to a PC via USB.

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

If you want to disallow any USB connection at all, then one way would be to query the USBManager for all the devices attached when your application starts via getDeviceList() and just verifying if the list size is 0 implying no device is attached. For any further connections, you can always listen for the intent ACTION_USB_ACCESSORY_ATTACHED and show a pop up if a device is attached during your application's lifecycle. So:

  1. Register for the intents ACTION_USB_ACCESSORY_ATTACHED / ACTION_USB_ACCESSORY_DETACHED on starting your application

  2. Query the USBManager via getDeviceList() and confirm the size of the returned list is 0.

  3. If not zero, wait for the user to detach the device and keep verifying via ACTION_USB_ACCESSORY_DETACHED till getDeviceList() doesn't return a zero size list.

  4. Keep monitoring the intents for USB activity and prompt user to detach the device as and when the devices are connected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM