Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BroadcastReceiver can't receive any broadcast events

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());

    }
}
like image 743
user121196 Avatar asked Jun 15 '09 06:06

user121196


3 Answers

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

like image 113
chiuki Avatar answered Oct 23 '22 00:10

chiuki


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

like image 30
Tughi Avatar answered Oct 22 '22 23:10

Tughi


If you register a broadcast receiver programmatically, you must also set the scheme to "file".

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
filter.addDataScheme("file");
mContext.registerReceiver(mExternalStorageReceiver, filter);
like image 34
Jörg Avatar answered Oct 22 '22 22:10

Jörg