Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using intent filters with broadcastReceivers Android

please how is the correct way to use broadcastReceiver in concert with Intent filters.. In my android_manifest.xml file I have those lines:

        <activity android:name=".DataDisplayActivity" android:theme="@android:style/Theme.Holo.NoActionBar" android:icon="@drawable/icon_3d" android:label="AdvancedHyperXPositiveSuperFluousApp">
        <intent-filter>
            <action android:name="com.simekadam.blindassistant.UPDATE_GPS_UI"/>
            <action android:name="com.simekadam.blindassistant.UPDATE_CONTEXT_UI"/> 
        </intent-filter>
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        </activity>

And in the activity I set the receiver with this function

    registerReceiver(broadcastReceiver, null);

It fails on the null, obvi it needs the IntentFilter to be set and I can add it inline as param to the function, but I asking, how to use it with XML defined intent filters..Thank for your help

Teaser: I actually got it working with the inline set intent, but I am asking how to make it working with the intent set in XML..

like image 481
simekadam Avatar asked Feb 16 '12 07:02

simekadam


1 Answers

you dont need to define intent-filters in your xml when you are using registerReceiver to receive broadcasts.

In your case, you should create a class which extends to BroadcastReceiver and then define that class file in your android's manifest file. for example:

class file:

package your.package.name;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyCustomReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if(action.equals("com.simekadam.blindassistant.UPDATE_GPS_UI")){
            //do something
        }
        else if(action.equals("com.simekadam.blindassistant.UPDATE_CONTEXT_UI")){
            //do something
        }
    }
}

and addition in manifest:

<receiver android:name=".MyCustomReceiver" android:enabled="true">
     <intent-filter>
        <action android:name="com.simekadam.blindassistant.UPDATE_GPS_UI" />
        <action android:name="com.simekadam.blindassistant.UPDATE_CONTEXT_UI" />
     </intent-filter>
</receiver>
like image 154
waqaslam Avatar answered Sep 28 '22 11:09

waqaslam