Android: detect when an SD card is inserted as a drive on a computer

I am writing an application that needs to be detected when the SD card is mounted as a disk to the computer via USB or when it was manually removed. I tried using a broadcast receiver for this purpose, but onReceive was not called. My code is as follows.

IntentFilter filter2 = new IntentFilter(); //filter2.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); filter2.addAction(Intent.ACTION_MEDIA_UNMOUNTED); filter2.addAction(Intent.ACTION_MEDIA_SHARED); filter2.addAction(Intent.ACTION_MEDIA_REMOVED); filter2.addAction(Intent.ACTION_MEDIA_MOUNTED); registerReceiver(new CustomBroadcastReceiver(), filter2); 

My broadcast receiver is as follows:

 public class CustomBroadcastReceiver extends BroadcastReceiver{ public CustomBroadcastReceiver(){ } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(Intent.ACTION_MEDIA_UNMOUNTED) || action.equals(Intent.ACTION_MEDIA_SHARED) || action.equals(Intent.ACTION_MEDIA_REMOVED)){ HardwareManager.IS_MEDIA_MOUNTED = false; }else if(action.equals(Intent.ACTION_MEDIA_MOUNTED)){ HardwareManager.IS_MEDIA_MOUNTED = true; }else if(action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)){ HardwareManager.IN_AIRPLANE_MODE = intent.getBooleanExtra("state", false); } } } 

The onReceive method does not work when I connect to the drive via USB.

What am I doing wrong?

+8
android broadcastreceiver android-sdcard
source share
3 answers

You need to add the β€œfile” scheme (addDataScheme (β€œfile”)) so that the IntentFilter matches ACTION_MEDIA_ * translation actions.

ACTION_MEDIA_ * intents has a path to the mount point in the Intent.mData field (see Intent docs ), but an IntentFilter without installed circuits will only match the intent if it does not contain data (see IntentFilter Documents ).

+11
source share

This is currently not supported at the SDK level.

Source: Android: detecting a USB flash drive connected to a network

+3
source share

Apparently ACTION_UMS_CONNECTED is not working. I need to work with API version 8, so any android.os.storage.StorageEventListener file is not applicable.

I used the answer posted here to update a static variable in a stream. It can also be used to perform custom broadcasting. The method is as follows: * NOTE. HardwareManager is a user-defined class.

 private static class SDCardMountStatusMonitor implements Runnable{ public void run() { // TODO Auto-generated method stub while(true){ try { HardwareManager.IS_MEDIA_MOUNTED = sdIsCardMounted(); //YOU CAN BROADCAST SOMETHING FROM HERE INSTEAD Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } public static boolean sdIsCardMounted() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { return false; } else { return false; } } 

Then, in another place, I create a thread from this Runnable and start it.

 Thread t = new Thread(new SDCardMountStatusMonitor()); t.start(); 
+1
source share

All Articles