Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 633
Jolin Avatar asked May 07 '13 05:05

Jolin


People also ask

What is the use of intent filter in the Androidmanifest XML file?

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.

What is the specific role of intent receiver and intent filter in broadcast receiver class?

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.

What is intent filter in XML?

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.


2 Answers

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.

like image 73
Karu Avatar answered Oct 13 '22 01:10

Karu


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>
like image 29
NMALKO Avatar answered Oct 12 '22 23:10

NMALKO