Android-Broadcast Receiver and Intent Filter

与世无争的帅哥 提交于 2019-11-29 14:35:22

问题


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 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.




回答2:


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);    



回答3:


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.



来源:https://stackoverflow.com/questions/3183756/android-broadcast-receiver-and-intent-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!