views:

202

answers:

1

Hi,

I am new to android platform.please help me out how the Broadcast Receiver and Intent Filter behaves in android.please explain in simple line or with example.thanks in advance...

+1  A: 

A broadcast receiver is a class in your Android project which is responsible to receive all intents, which are sent by other activities by using android.content.ContextWreapper.sendBroadcast(Intent intent)

In the manifest file of you receicving activity, you have to declare which is your broadcast receiver class, for example:

<receiver android:name="xyz.games.pacman.network.MessageListener">
         <intent-filter>
            <action android:name="xyz.games.pacman.controller.BROADCAST" />
            </intent-filter>
         </receiver>

As you can see, you also define the intent filter here, that is, which intents should be received by the broadcas receiver.

Then you have to define a class which extends BroadcastReceiver. This is the class you defined in the manifest file:

public class MessageListener extends BroadcastReceiver {


    /* (non-Javadoc)
     * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
     */
    @Override
    public void onReceive(Context context, Intent intent) {
...
}

Here, all intents which are passed through the filter are received and you can access them using the parameter passed in the method call.

Roflcoptr