tags:

views:

2565

answers:

3

I have created a BroadcastReceiver to detect SDCard mount and unmount event, however, I am not able to receive any events at all: here's the AndroidManifest.xml:

    <receiver android:enabled="true" android:label="SDCardMountReceiver"
                android:exported="true"
                android:name="xxx.broadcasts.SDCardBroadcastReceiver">
                        <intent-filter>
                                <action
android:name="android.content.Intent.ACTION_MEDIA_MOUNTED"></action>
                               <!-- or  <action
android:name="android.content.Intent.ACTION_MEDIA_UNMOUNTED" />-->

                        </intent-filter>
    </receiver>

And the SDCardMountReceiver class:

public class SDCardBroadcastReceiver extends BroadcastReceiver {
        public SDCardBroadcastReceiver(){
                super();
                System.err.println("constructor");
        }

        public void onReceive(Context context, Intent intent) {
                Log.d("SDCardBroadCastReceiver", "receive "+intent.getAction());
                System.err.println("jonathan receive "+intent.getAction());

        } 
+2  A: 

The Intent javadoc specifies a different action:name value. Use "android.intent.action.MEDIA_MOUNTED" instead of "android.content.Intent.ACTION_MEDIA_MOUNTED"

Tughi
I have tried the actualy string constant,android.intent.action.MEDIA_MOUNTED, but still not able to receive the broadcast. other thoughts?
I have tried creating the receiver dynamically in oncreate() on my Activity class, yet still nothing is received in onReceive of my SDCardBroadcastReceiver.<pre>SDCardBroadcastReceiver myReceiver = new SDCardBroadcastReceiver(); IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); registerReceiver(myReceiver, filter);</pre>
+3  A: 

You also need to set the data scheme to "file".

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

Reference: android-developers thread

chiuki
Thanks for that! Is that documented anywhere at all?
Alex Pretzlav
A: 
Alex