Search code examples
androidsd-cardbroadcastlisten

android: how to listen to "sd card removed unexpectedly"


I have a program that uses content from sd-card. I want to listen to different states like sd-card mounted or sd-card removed unexpectedly. How can I do so. An example would be of a great help.

Thanks to all


Solution

  • You need to listen for ACTION_MEDIA_REMOVED and ACTION_MEDIA_MOUNTED. Create a receiver and listen for this action.

    EDIT:

    In your manifest file add this

    <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_REMOVED" />
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
            <data android:scheme="file" />
        </intent-filter>
    </receiver>
    

    then create a class MyReceiver which will extend BroadcastReceiver and then catch these actions and perform what you want to do.