Farhan
Farhan

Reputation: 3226

Android: how to know when SD-card is un-mounted

I am working on an application that reads media from the sd-card and plays it.. However, there are certain events when the sd-card is physically removed from the device or the device is attached to a computer and the sd-card is mounted on the computer.. Whenever the sd-card is removed, a notification is generated and can be seen in the notification bar. I want my application to respond to those notifications. For example, whenever the user physically removes the card or attaches it to the computer, I want my application to display a message like "sd card removed".. Is there any way to respond to these notifications??

Upvotes: 1

Views: 1087

Answers (2)

Ena
Ena

Reputation: 3611

I know question is old but I ended up here looking for this code, so here it is.

In the manifest.xml register the receiver:

<receiver android:name="it.myapp.receiver.OnMediaMountedReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file"/>
    </intent-filter>
</receiver>

Then create receiver class:

public class OnMediaMountedReceiver extends BroadcastReceiver {

private static final String TAG = OnMediaMountedReceiver.class.getSimpleName();

@Override
public void onReceive(Context arg0, Intent intent) {
    Log.i(TAG, "Media mounted!");
    if (intent != null && intent.getData() != null) {
        Log.i(TAG, "Media path: " + intent.getDataString());
    }
    }
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006614

I want my application to respond to those notifications.

No, you don't. You want to respond to the broadcast Intents that are also used to create those Notifications.

Look at the documentation for the Intent class, at the ACTION_MEDIA_* set of broadcast actions.

Upvotes: 2

Related Questions