开发者

Android-Broadcast Receiver and Intent Filter

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 advan开发者_JS百科ce...


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.


A BroadcastReceiver can be registered in two ways: dynamic or static. Static is nothing but declaring the action through an intent-filter in AndroidManifest.xml to register a new BroadcastReceiver class. Dynamic is registering the receiver from within another class. An intent-filter determines which action should be received.

To create a BroadcastReceiver, you have to extend the BroadcastReceiver class and override onReceive(Context,Intent) method. Here you can check the incoming intent with Intent.getAction() and execute code accordingly.

As a new class, static would be

public class Reciever1 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        String str = intent.getAction();
        if(str.equalsIgnoreCase("HELLO1")) {
            Log.d("Abrar", "reciever....");             
            new Thread() {
                public void run() {                     
                    Log.d("Abrar", "reciever....");
                    System.out.println("Abrar");                        
                }
            }.start();                          
        }

or, if placed inside an existing class, it is called dynamically with

intentFilter = new IntentFilter();
intentFilter.addAction("HELLO1");

//---register the receiver---
registerReceiver(new Reciever1(), intentFilter);    


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜