Broadcast被分成两种:
1.Normal Broastcast(普通广播)
Normal Broadcast是完全异步的,可以在同一时刻(逻辑上)被所有接收者收到,消息传递的效率比较高。但缺点是接收者不能将处理结果传递给下一个接收者,并且无法终止Broadcast Intent的传播。
2.Ordered Broadcast(有序广播)
Ordered Broadcast的接收者将按预先声明的优先级依次接收Broadcast。如:A的级别高于B,B的级别高于C。那么,Broadcast先传给A,再传给B,最后传给C。优先级声明在<intent-filter.../>元素的android:priority属性中,数越大优先级别越高,取值范围为-1000~1000,优先级也可以调用IntentFilter对象的setPriority()进行设置。Ordered Broadcast接收者可以终止Broadcast Intent的传播,Broadcast Intent的传播一旦终止,后面的接收者就无法接收到Broadcast。另外,Ordered Broadcast的接收者可以将数据传递给下一个接收者,如:A得到Broadcast后,可以往它的结果对象中存入数据,当Broadcast传给B时,如:A得到Broadcast后,可以往它的结果对象中存入数据,当Broadcast传给B时,B可以从A的结果对象中得到A存入的数据。
Context提供的如下两个方法用于发送广播。
1.sendBroadcast():发送Normal Broadcast。
2.sendOrderedBroadcast():发送Ordered Broadcast。
对于Ordered Broadcast而言,系统会根据接收者声明的优先级按顺序逐个执行接收,优先接收到Broadcast的接收者可以终止Broadcast,调用BroadcastReceiver的abortBroadcast()方法即可终止Broadcast。如果Broadcast被前面的接收者终止,后面的接收者就再也无法接收到Broadcast了。
不仅如此,对于Ordered来说,优先接收到Broadcast的接收者可以通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,然后传给下一个接收者,下一个接收者通过代码:Bundle bundle = getResultExtras(true)可以获取上一个接收者存入的数据。
/********************************************************/
下面是一个实例:
工程结构:
全部代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.leidong.sortedbroadcast">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="1">
<action android:name="com.example.leidong.action.OrderedBroadcast"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="0">
<action android:name="com.example.leidong.action.OrderedBroadcast"/>
</intent-filter>
</receiver>
</application>
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#2b2b2b">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5pt"
android:layout_marginRight="5pt"
android:layout_marginTop="100pt"
android:text="S E N D"
android:textColor="#ffffff"
android:textSize="10pt"
android:textStyle="bold"
android:background="#696969"
android:id="@+id/sendButton"
android:layout_gravity="center_horizontal" />
</LinearLayout>
MainActivity.java
package com.example.leidong.sortedbroadcast;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
/**
* Created by leidong on 2016/9/6.
*/
public class MainActivity extends AppCompatActivity {
Button sendButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sendButton = (Button)findViewById(R.id.sendButton);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction("com.example.leidong.action.OrderedBroadcast");
intent.putExtra("msg","HaHaHaHaHa!!!");
sendOrderedBroadcast(intent, null);
}
});
}
}
MyReceiver1.java
package com.example.leidong.sortedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
/**
* Created by leidong on 2016/9/6.
*/
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,
"接收到的Intent的Action为:" + intent.getAction() + "\n 消息内容是:" + intent.getStringExtra("msg"),
Toast.LENGTH_LONG).show();
//创建一个Bundle对象并存入数据
Bundle bundle = new Bundle();
bundle.putString("first", "LaLaLaLa!!!");
//将Bundle放入结果中
setResultExtras(bundle);
}
}
MyReceiver2.java
package com.example.leidong.sortedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
/**
* Created by leidong on 2016/9/6.
*/
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = getResultExtras(true);
//解析前一个BroadcastReceiver所存入的key为first的消息
String first = bundle.getString("first");
System.out.println(first);
Toast.makeText(context,
"我是第二个BroadcastReceiver,我收到的消息是:" + first,
Toast.LENGTH_LONG).show();
}
}
/************************************************************/
AVD运行:
点击SEND按钮后:
等待几秒后: