I am writing an application that needs to detect when the SD card is mounted as a disk drive to a computer via USB or when it has been manually removed. I tried using a broadcast receiver for this purpose, but the onReceive is not getting 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 fire when I connect as a disk drive via USB.
What am I doing wrong ?
Seemingly the ACTION_UMS_CONNECTED does not work. I need to work on API version 8, hence any android.os.storage.StorageEventListener is not applicable.
I used the answer posted here to update a static variable in a thread. This can also be used to carry out a custom broadcast as well. 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 out of this Runnable and start it.
Thread t = new Thread(new SDCardMountStatusMonitor());
t.start();
You have to add the "file" scheme (addDataScheme("file")) in order for the IntentFilter to match the broadcast ACTION_MEDIA_* action intents.
The ACTION_MEDIA_* intents have the path to the mount point in Intent.mData field (see Intent docs) but an IntentFilter with no schemes set will only match an intent if it includes no data (see IntentFilter docs).
This is currently not supported at SDK level.
Source: Android: Detect USB flash drive plugged in
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With