Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:51:20+00:00 2026-05-26T01:51:20+00:00

im trying to do something similar to this: android: how to listen to "sd

  • 0

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!.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:51:21+00:00Added an answer on May 26, 2026 at 1:51 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to do something similar to this but in Android. In Android
This is what I'm trying to do something similar to the following request, but
I am trying to do something similar to this, but not sure how to
I'm trying to write something similar to this (sorry if the sample is not
I am trying to do something very similar to this c# docs example :
I'm trying to insert a comment character into a string something similar to this:
I'm trying to implement something similar to what http://www.ancestry.com has, but I'm not sure
Using SQL Server 2008, I am trying to do something similar to this post
I'm trying to do something similar to this post where I don't pull back
I'm trying to achieve something similar to this: public void execute(){ RandomClass item =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.