多个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来接收同一个消息的方法。

 

 

 

 

 

 

posted @ 2013-04-14 22:22  Mr轨迹  阅读(1462)  评论(0编辑  收藏  举报