7

Is there a way to detect when a USB flash drive is plugged into an Android device? I'm able to detect an SD card using a broadcast receiver, but it doesn't work for USB. I'd like to avoid polling.

code to register receiver:

private void RegisterUpdateReceiver()
{
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("android.intent.action.MEDIA_MOUNTED");
    intentFilter.addDataScheme("file");
    myReceiver = new MyReceiver();
    this.registerReceiver(myReceiver, intentFilter);
}

receiver code:

public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if (action.equals("android.intent.action.MEDIA_MOUNTED")) 
        {
            // react to event
        }
}
Ravi
  • 3,718
  • 7
  • 39
  • 57
  • 1
    The code you posted in your question works perfectly for me to detect an usb drive (via USB OTG) on a Allwinner Development board with Android 6 – OneWorld Apr 10 '18 at 07:41

4 Answers4

3

If detecting attaching and detaching the USB will work "android.hardware.usb.action.USB_DEVICE_ATTACHED" can be used. Make sure the definition of the receiver and the intent filter is added in the manifest as well.

Malaka
  • 107
  • 1
  • 6
  • On my smartphone that event came before the drive was mounted. I have wait for a second before I can use the drive. – OneWorld Apr 09 '18 at 15:03
2

This is the xml version of the receiver in the AndroidManifest.xml:

        <receiver
            android:name="MyMountBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_MOUNTED"/>
                <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
                <data android:scheme="file" />
            </intent-filter>

        </receiver>
OneWorld
  • 17,512
  • 21
  • 86
  • 136
2

This is how I made it work on Android 6.0. Basically, what we need to detect is when the drive is mounted which can be done by ACTION_MEDIA_MOUNTED that is "android.intent.action.MEDIA_MOUNTED".

Follow the steps below:

  1. Register a broadcast receiver for "ACTION_USB_DEVICE_ATTACHED"
  2. Register a broadcast receiver for "ACTION_MEDIA_MOUNTED"
  3. Seek permission for using the device when "ACTION_USB_DEVICE_ATTACHED" action is received. Then you will receive broadcast for "ACTION_MEDIA_MOUNTED"

You can refer: https://developer.android.com/reference/android/content/Intent#ACTION_MEDIA_MOUNTED

Note: Registering broadcast receiver for ACTION_MEDIA_MOUNTED alone does not work. So used broadcast "ACTION_USB_DEVICE_ATTACHED" for permission purpose.

pnk
  • 35
  • 1
  • 14
2

Android, at the SDK level, has no concept of USB drives. There are no rules for where they should be mounted, broadcasts for when they appear/disappear, etc. Perhaps some standardization in this area will come in future Android releases, but it is not there today.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Logcat shows some events relating to USB mount, but I can't detect this at the SDK level. Sounds like what you're saying is right. W/MountService( 931): getVolumeState(/mnt/usbdisk): Unknown volume E/VoldConnector( 931): Error handling '605 Volume usbdisk /mnt/usbdisk state changed from 3 (Checking) to 4 (Mounted)' – Ravi Nov 11 '11 at 01:46
  • 1
    any luck here, I also use ACTION_MEDIA_REMOVED and other receiver related USB mount and unmount...but nothing is working..but in command prompt it showing usb log? – CoDe Dec 04 '12 at 13:23
  • 1
    @Shubh: The answer as written is still accurate, as of Android 4.2. – CommonsWare Dec 04 '12 at 13:33
  • 3
    I got answer..I added tag in receiver class..and it's working for me. – CoDe Dec 04 '12 at 14:00
  • @CommonsWare Is there something of the opposite? When I connect the device to the PC via USB, and choose "transfer files" instead of charging it, can I get any Intent, or even get the current status of it (charging vs transfer-files) ? Or maybe it's the same? – android developer Apr 20 '20 at 23:47
  • 1
    @androiddeveloper: Sorry, but I have no idea. – CommonsWare Apr 21 '20 at 12:00
  • @CommonsWare Thank you for answering nevertheless. – android developer Apr 21 '20 at 12:01