Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listener for sd-card removal

Tags:

android

im trying to do something similar to this: android: how to listen to "sd card removed unexpectedly" but onReceive of the listener never gets called, when i dont have sdcard mounted or i remove the sdcard. Here is the code.

public class MyClass1  extends Activity{
    BroadcastReceiver mSDCardStateChangeListener = null;
     /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    mSDCardStateChangeListener = MyClass2.registerSDCardStateChangeListener(this);
   //some code which needs SDCard and throws unhandled exception if sdcard is not there 

}

@Override 
    protected void onDestroy () 
    {
        MyClass2.unRegisterSDCardStateChangeListener(this, mSDCardStateChangeListener);
        super.onDestroy();
    }



//in MyClass2
public static BroadcastReceiver registerSDCardStateChangeListener(Activity act) {

        BroadcastReceiver mSDCardStateChangeListener = new BroadcastReceiver() {

            @Override
            public void onReceive(Context arg0, Intent arg1) {
                 String action = arg1.getAction();
                    if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED)
                            || action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED)
                            || action.equalsIgnoreCase(Intent.ACTION_MEDIA_BAD_REMOVAL)
                            || action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) 
                    {
                        //i never come here ;(
                    //do something

                    }

            }
        };
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
        filter.addAction(Intent.ACTION_MEDIA_EJECT);
        filter.addDataScheme("file");
        act.registerReceiver(mSDCardStateChangeListener, filter);
        return mSDCardStateChangeListener;
 }

public static void unRegisterSDCardStateChangeListener(Activity act, BroadcastReceiver mSDCardStateChangeListener)
     {
         act.unregisterReceiver(mSDCardStateChangeListener);
     }

i do not want to check if sdcard is present or not by if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) but use receiver instead. Any help is welcome.Thanks!.

like image 262
con_9 Avatar asked Sep 26 '11 08:09

con_9


People also ask

Can you just remove an SD card?

Tap the Unmount SD Card button. And then tap OK to confirm in the pop-up that appears. The SD card will unmount, and a notification will appear saying, “SD card safe to remove. You can safely remove SD card.” You can now pull it out of your phone or tablet and not risk losing any data.

How do I access my SD card on Android phone?

On your device, open your Settings. Storage. Check if your SD card is recognized. If the SD card isn't recognized in Settings: Remove and reinsert the SD card.

How do I access the SD card on my Samsung?

Android - SamsungFrom any Home screen, tap Apps. Tap My Files. Tap Device storage. Navigate inside your device storage to the files you want to move to your external SD card.


1 Answers

Ok I think the code I posted is meant for the action & not the state & works fine.

from documentation:

android.content.Intent.ACTION_MEDIA_REMOVED Broadcast Action: External media has been removed. The path to the mount point for the removed media is contained in the Intent.mData field.

so what I was expecting(I was wrong, see the first two lines of the Question) that if i dont have SDCard(i.e. it has been removed earlier) and then I launch the app I would get the call implying that I dont have the SDCard (I know sounds stpid ;)) . The intent are actions(and not state).So If I remove the sdcard while the app is active I do receive the callback. Thanks for your time Vegas.

like image 117
con_9 Avatar answered Oct 30 '22 03:10

con_9