Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to Detect "Turn on USB storage" Broadcast?

I am trying to detect the Turn on USB storage using BroadcastReceiver though i am able to detect the USB connected using android.intent.action.UMS_CONNECTED action
and
disconnected using android.intent.action.UMS_DISCONNECTED action.


How can i detect the USB storage ?

like image 853
Basbous Avatar asked Jan 09 '12 16:01

Basbous


1 Answers

Below is how I check if storage card is mounted/unmounted. You can change it to check removed/insterted. I do this by register a BroadcastReceiver to get the "mount events" then check what state the storage card is in. If it is not mounted and is not while it is checking (the state during it mounts the card again) it is unmounted or the card has been removed.

public class MemCardReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            onMemcardMounted();
        }
        else if (!Environment.getExternalStorageState().equals(Environment.MEDIA_CHECKING)){
            onMemorycardUnMounted();
        }
    }

    private void onMemorycardUnMounted() {}

    private void onMemcardMounted() {}
}

And in ManifestFile

<receiver android:enabled="true" android:exported="true" android:name="the.name"> 
        <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>

There are several different states checkout this if there are any other stated like. removed

like image 74
giZm0 Avatar answered Oct 04 '22 08:10

giZm0