Android-广播接收器和意图过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3183756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:03:38  来源:igfitidea点击:

Android-Broadcast Receiver and Intent Filter

androidbroadcastreceiverintentfilter

提问by bharathi

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

我是 android 平台的新手。请帮助我了解广播接收器和意图过滤器在 android 中的行为。请用简单的行或示例进行解释。提前谢谢...

回答by RoflcoptrException

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)

广播接收器是您的 Android 项目中的一个类,它负责接收所有意图,这些意图是由其他活动通过使用 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:

然后你必须定义一个扩展 BroadcastReceiver 的类。这是您在清单文件中定义的类:

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.

在这里,通过过滤器传递的所有意图都被接收,您可以使用方法调用中传递的参数访问它们。

回答by Abrar Ahmad Khan

A BroadcastReceiver can be registered in two ways: dynamicor static. Static is nothing but declaring the action through an intent-filterin AndroidManifest.xmlto register a new BroadcastReceiverclass. Dynamic is registering the receiver from within another class. An intent-filterdetermines which action should be received.

BroadcastReceiver 可以通过两种方式注册:dynamicstatic. 静态只不过是通过intent-filterin声明操作AndroidManifest.xml以注册新的BroadcastReceiver类。动态正在从另一个类中注册接收器。Anintent-filter确定应该接收哪个动作。

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.

要创建 BroadcastReceiver,您必须扩展 BroadcastReceiver 类和覆盖onReceive(Context,Intent)方法。在这里,您可以检查传入的意图Intent.getAction()并相应地执行代码。

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

回答by Rohit Mandiwal

BroadcastReceiver: 'Gateway' with which your app tells to Android OS that, your app is interested in receiving information.

BroadcastReceiver:您的应用通过“网关”告诉 Android 操作系统,您的应用有兴趣接收信息。

Intent-Filter: Works with BroadcastReceiverand tells the 'What' information it is interested to receive in. For example, your app wants to receive information on Battery level.

Intent-Filter:使用BroadcastReceiver并告诉它有兴趣接收的“什么”信息。例如,您的应用程序想要接收有关电池电量的信息。