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>
The intent filter specifies the types of intents that an activity, service, or broadcast receiver can respond. Intent filters are declared in the Android manifest file.
BroadcastReceiver : 'Gateway' with which your app tells to Android OS that, your app is interested in receiving information. Intent-Filter : Works with BroadcastReceiver and tells the 'What' information it is interested to receive in. For example, your app wants to receive information on Battery level.
An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.
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.
Registering in Android Manifest file.
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
Registering programmatically.
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Create the receiver
receiver = new TimelineReceiver();
filter = new IntentFilter( UpdaterService.NEW_STATUS_INTENT );
}
protected void onResume() {
super.onResume();
super.registerReceiver(receiver, filter,
"com.marakana.yamba.SEND_TIMELINE_NOTIFICATIONS", null);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
...
UPDATE: Multiple values If more than one value can be specified, the element is almost always repeated, rather than listing multiple values within a single element. For example, an intent filter can list several actions:
<intent-filter . . . >
<action android:name="android.intent.action.EDIT" />
<action android:name="android.intent.action.INSERT" />
<action android:name="android.intent.action.DELETE" />
. . .
</intent-filter>
UPDATE2: That's an example of AndroidManifest.xml
<manifest
package="com.marakana.android.lifecycle"
android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:name=".ApplicationDemo"
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".ActivityDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnotherActivity"></activity>
<activity android:name=".SystemServicesDemo"></activity>
<service android:name=".ServiceDemo"></service>
<service android:name=".IntentServiceDemo">
<intent-filter>
<action android:name="marakana.intent.action.IntentServiceDemo" />
</intent-filter>
</service>
<receiver android:name=".ReceiverDemo">
<intent-filter>
<action android:name="marakana.intent.action.ReceiverDemo" />
</intent-filter>
</receiver>
<provider
android:name=".ProviderDemo"
android:authorities="com.marakana.android.lifecycle.providerdemo" />
</application>
</manifest>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With