广播接收器类和registerReceiver方法 [英] Broadcast Receiver class and registerReceiver method

查看:308
本文介绍了广播接收器类和registerReceiver方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想了解广播接收器,我经历了很多样品codeS,但仍然有一些疑问。我想知道,当我们不得不延长广播接收器类时,我们应该使用 registerReceiver()方法时,我们应该创建对象的BroadcastReceiver。在一些程序中,我遇到了 registerReceiver 正在使用的方法,但没有扩展广播接收器类。我也想知道如何使用的onReceive 方法被调用。

哪种方法时,应使用?

这里是 registerReceiver 方式:

  registerReceiver(新BroadcastReceiver的(){

            @覆盖
            公共无效的onReceive(上下文的背景下,意图意图){
                开关(的getResult code()){
                ........
                }

            }

        },新的IntentFilter(发送));
 

对象正在为创建的BroadcastReceiver

 私人的BroadcastReceiver intentReceiver =新的BroadcastReceiver(){

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        .................
    }

};
 

解决方案

Android有意图的行动对于广播接收机。 广播接收机将触发时听这在它注册的任何行动。

现在,我们将举一个例子: 我们需要倾听的动作,只要任何蓝牙设备连接到我们的设备。对于Android有其固定的动作 android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED

所以,你可以通过舱单和放大器得到它;注册也

舱单报名方式:

在你的清单将这个

 <接收器的Andr​​oid版本:NAME =MyBTReceiver>
    <意向滤光器>
                <作用机器人:名称=android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED/>
      &所述; /意图滤光器>
< /接收器>
 

创建 MyBTReceiver.class

 公共类MyBTReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){

        如果(intent.getAction()。等于(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED)){
            Log.d(TAG,蓝牙连接);
                                                                          }
                                                           }
}
 

这是最简单的广播接收机。

现在,  如果你只对收到广播你正在运行时,最好是使用registerReceiver()。您还可以<一href="http://developer.android.com/reference/android/content/Context.html#registerReceiver%28android.content.BroadcastReceiver,%20android.content.IntentFilter%29">register它在现有的类文件。你还需要<一个href="http://developer.android.com/reference/android/content/Context.html#unregisterReceiver%28android.content.BroadcastReceiver%29">unregister它的onDestroy()。 在这里,你不需要任何广播登记清单中,除了活动登记

例如

 公共类MainActivity扩展活动
    {
        IntentFilter的过滤器1;
          @覆盖
          公共无效的onCreate()
          {

             过滤器1 =新的IntentFilter(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);

                registerReceiver(myReceiver,过滤器1);
        }
        //监听蓝牙广播的BroadcastReceiver
    私人最终的BroadcastReceiver myReceiver =新的BroadcastReceiver(){

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){
            // TODO自动生成方法存根

            如果(intent.getAction()。equalsIgnoreCase(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED))
            {
                Log.d(TAG,蓝牙连接);
            }

        }
    };
    @覆盖
    公共无效的onDestroy(){

        unregisterReceiver(myReceiver);

    }
    }
 

Hi i am trying to understand Broadcast Receiver , i went through many sample codes , but still have some doubts. I wanted to know when we have to extend the Broadcast Receiver class and when should we use registerReceiver() method and when should we create object for BroadcastReceiver. In some programs i came across registerReceiver methods being used but without extending the Broadcast Receiver class. I also wanted to know how the onReceive method gets called.

Which approach should be used when?

here is the registerReceiver method:

registerReceiver(new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                switch (getResultCode()) {
                ........
                }

            }

        }, new IntentFilter(SENT));

Object being created for BroadcastReceiver:

private BroadcastReceiver intentReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        .................
    }

};

解决方案

Android has intent action for broadcast receiver. BroadCast receiver will be trigger when it listen any action which registered within it.

Now we will take one example : That we need to listen the action of "whenever any bluetooth device connect to our device". For that android has it fix action android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED

So you can get it via manifest & registration also

BY Manifest Registration:

Put this in your manifest

<receiver android:name="MyBTReceiver">
    <intent-filter>
                <action android:name="android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED" />
      </intent-filter>
</receiver>

Create MyBTReceiver.class

public class MyBTReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED")){
            Log.d(TAG,"Bluetooth connect");
                                                                          }
                                                           }
}

That was the simplest broadcast Receiver.

Now, if you are only interested in receiving a broadcast while you are running, it is better to use registerReceiver(). You can also register it within your existing class file. you also need to unregister it onDestroy(). here, you dont need any broadcast registration in manifest except activity registration

For example

    public class MainActivity extends Activity
    {
        IntentFilter filter1;
          @Override
          public void  onCreate()
          {

             filter1 = new IntentFilter("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED");

                registerReceiver(myReceiver, filter1);
        }
        //The BroadcastReceiver that listens for bluetooth broadcasts
    private final BroadcastReceiver myReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            if(intent.getAction().equalsIgnoreCase("android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED"))
            {
                Log.d(TAG,"Bluetooth connect");
            }

        }
    };
    @Override
    public void onDestroy() {

        unregisterReceiver(myReceiver);

    }
    }

这篇关于广播接收器类和registerReceiver方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆