How to register a custom Intent filter to a broadcast receiver in AndroidManifest.xml?

Jolin picture Jolin · May 7, 2013 · Viewed 32.2k times · Source

I have defined a receiver in AndroidManifest.xml to receive a PlAY_FINISHED action, and in other file I send an intent to that broadcast receiver like follows:

public String PlAY_FINISHED = "play finished"; 
...
Intent in = new Intent(PlAY_FINISHED);
this.service.sendBroadcast(in);

so in my manifest file, i set it like this, where MyStaticString is a class that contains all the static string in the application. Is this the correct way?

    <intent-filter>
        <action android:name="com.mysite.appname.MyStaticString.PLAY_FINISHED" />
    </intent-filter>

Answer

Karu picture Karu · Sep 22, 2014

The android:name of an intent filter in the manifest is just an arbitrary string, not the "path" to a Java constant. The problem is that your string constant in code is defined as "play finished", which doesn't match the name "com.mysite.appname.MyStaticString.PLAY_FINISHED" that you've specified in the manifest.

It should be

public String PlAY_FINISHED = "com.mysite.appname.MyStaticString.PLAY_FINISHED";

It doesn't matter what the variable is called, or even if you store the string in a variable at all. Or that its name contains a typo :)

You could instead change the android:name in the manifest to "play finished", but custom broadcast actions are system-wide so they should be qualified with the package name of your app to avoid collisions with other apps.