android: how to listen to "sd card removed unexpectedly"
Asked Answered
K

4

9

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

Kilt answered 7/8, 2011 at 15:43 Comment(0)
G
16

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.

Gunrunning answered 7/8, 2011 at 16:57 Comment(2)
Sir, Can you elaborate a little more on that. I am a beginner in android. An example here would greatly help me.. thanks alotKilt
also need add <data android:scheme="file" /> in intent-filterVandavandal
H
2

Create a receiver in Manifest:

    <receiver android:name=".ExternalSDcardRemoved">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_EJECT" />
            <data android:scheme="file" />
        </intent-filter>
    </receiver>

And a corresponding class file:

public class ExternalSDcardRemoved extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // SD card removed
    }
}
Honolulu answered 9/2, 2016 at 17:4 Comment(0)
P
1

You can use something like this:

    static boolean checkSdCardStatus(final Activity activity) {

    String status = Environment.getExternalStorageState();
    //the SD Card is mounted as read-only, but we require it to be writable.
    if (status.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        UIMethods.showFinalAlert(activity, R.string.sdcard_readonly);
        return false;
    }
    //your handset is mounted as a USB device
    if (status.equals(Environment.MEDIA_SHARED)) {
        UIMethods.showFinalAlert(activity, R.string.sdcard_shared);
        return false;
    }
    //no SD Card inserted
    if (!status.equals(Environment.MEDIA_MOUNTED)) {
        UIMethods.showFinalAlert(activity, R.string.no_sdcard);
        return false;
    }

    return true;
}

And call this method in Activity.onStart() or in Activity.onResume().

Phobe answered 7/8, 2011 at 15:52 Comment(1)
actually I am looking for another way i.e., broadcast receiver.I am not sure, how to implement a broadcast receiver. Can you help me here?Kilt
S
1

Thanks to @PravinCG

Here is the complete code.

SDCardBroadcastReceiver.java code

public class SDCardBroadcastReceiver extends BroadcastReceiver {


    private static final String ACTION_MEDIA_REMOVED = "android.intent.action.MEDIA_REMOVED";
    private static final String ACTION_MEDIA_MOUNTED = "android.intent.action.MEDIA_MOUNTED";
    private static final String MEDIA_BAD_REMOVAL = "android.intent.action.MEDIA_BAD_REMOVAL";
    private static final String MEDIA_EJECT = "android.intent.action.MEDIA_EJECT";
    private static final String TAG = "SDCardBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {



        Log.i(TAG, "Intent recieved: " + intent.getAction());

        if (intent.getAction() == ACTION_MEDIA_REMOVED) {

            Log.e(TAG, "ACTION_MEDIA_REMOVED called");

            // For bundle Extras do like below
//            Bundle bundle = intent.getExtras();
//            if (bundle != null) {
//
//            }
        }else if (intent.getAction() == ACTION_MEDIA_MOUNTED){

            Log.e(TAG, "ACTION_MEDIA_MOUNTED called");

        }else if(intent.getAction() == MEDIA_BAD_REMOVAL){

            Log.e(TAG, "MEDIA_BAD_REMOVAL called");

        }else if (intent.getAction() == MEDIA_EJECT){

            Log.e(TAG, "MEDIA_EJECT called");

        }
    }
}

and here is my manifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="genetechsolutions.sdcardmountlistner">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


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

    </application>

</manifest>
Sagunto answered 21/3, 2017 at 14:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.