12

I'm baffled. I'm trying to configure my app to respond to the SD card becoming available / going offline, but my broadcast receiver never gets called!

I can see the event being broadcasted, and other apps responding:

08-21 23:43:04.405: DEBUG/Ringer(275): -- intent.getAction() =android.intent.action.MEDIA_MOUNTED

And my manifest has the receiver declared:

    <receiver android:name=".Test" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_MOUNTED" />
        </intent-filter>
    </receiver>

And my receiver has an onReceive method:

public class Test extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("#########", "##############################################################");
        Log.d("#########", "Obligitory snarky and/or funny logging comment...");
        Log.d("#########", "##############################################################");
    }
}

Yet the &^%$'ing thing won't cause Test.onReceive() to fire. Any thoughts?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Skylar Sutton
  • 4,632
  • 3
  • 27
  • 39

2 Answers2

48

You can't be serious. Apparently I needed to add an additional filter for the data type.

Leaving the answer up for "the next guy"...

<receiver android:name=".Test" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_MOUNTED" />
        <data android:scheme="file"/>
    </intent-filter>
</receiver>
Skylar Sutton
  • 4,632
  • 3
  • 27
  • 39
  • 1
    Looks like the official docs are missing the scheme, which is certainly annoying since their code won't actually work. http://developer.android.com/reference/android/os/Environment.html – smith324 Dec 29 '11 at 22:12
  • 2
    For people linking receivers dynamically you can call addDataScheme("file") on your IntentFilter instance. – dhakim Apr 18 '14 at 14:57
7

You try adding the <data android:scheme="file" /> tag in the <intentfilter>, else do the registration at run time.
At run time, create an object of your broadcast receiver and pass it to registerReceiver(obj).

Ron
  • 24,175
  • 8
  • 56
  • 97