zoukankan      html  css  js  c++  java
  • 多个BroadCastReceiver同时匹配同一类Intent发送的消息的情况

    如总体的下图:



    接下来是贴代码了:


    package com.example.broadcasttest;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    public class BroadCastReceiver1 extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    Log.v("Show_V", "I am Receiver-1 !");
    }
    }
    package com.example.broadcasttest;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    public class BroadCastReceiver2 extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.v("Show_V", "I am Receiver-2 !");
    }
    }
    package com.example.broadcasttest;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent();
    intent.setAction("MyAction");
    sendBroadcast(intent);
    }
    }



    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.broadcasttest"
        android:versionCode="1"
        android:versionName="1.0" >
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="16" />
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.broadcasttest.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".BroadCastReceiver1" >
                <intent-filter>
                    <action android:name="MyAction" />
                </intent-filter>
            </receiver>
            <receiver android:name=".BroadCastReceiver2" >
                <intent-filter>
                    <action android:name="MyAction" />
                </intent-filter>
            </receiver>
        </application>
    </manifest>




    代码的基本机理是:

    通过Manifest文件注册了2Receiver对象,并在<intent-filter>标签对里面对消息进行了过滤,意思是:当前注册的Receiver对象只对我定义的消息类型进行相应,也就是:MyAction

    其中我对这两个Receiver都进行了消息过滤,也就是只监听MyAction这个消息。再看BroadCastReceiver1,里面的代码实现很简单,就是简单的继承了BroadcastReceiver这个父类,然后实现未实现的方法onReceiver()就可以了。程序运行以后的结果是:

    其中还有需要进一步用代码来验证的东西,也就是谁会先接受到这个intent发送过来的消息呢,我们对广播的接收顺序并没有设置什么,其实我通过调整Manifest文件里面的广播注册顺序,运行结果的接收顺序就改变了。通过扩展,我们可以进一步对intent来做功能,比如发送一定的数据给广播,通过intent.putExtra();来实现,在然后再在广播的onReceiver()方法里面对收到的intent来进行提取消息,也就是intent.getXXXExtra();XXX是你想提取的类型。

    这样我们就实现了多个Receiver来接收同一个消息的方法。

  • 相关阅读:
    vim编辑器替换以及全局替换
    Linux下grep显示前后几行信息
    Pymol里常用到的命令,随用随记
    硬盘里有文件错误,导致删除不了文件,可以使用如下方法
    解决Host key verification failed
    tcl语言杂记
    python脚本后台运行的几种方式
    centos设置连续登录3次密码错误自动锁定账户3分钟
    ubuntu安装显卡驱动
    虚拟交换机(OVS)之结构印象
  • 原文地址:https://www.cnblogs.com/vokie/p/3602091.html
Copyright © 2011-2022 走看看