一、实验目的
掌握有序广播的使用
二、实验仪器
Andriod Studio
三、实验任务
运行程序,解决以下三个问题:
问题1 :程序启动后,单击“发送有序广播”按钮,发出一条广播事件,此时观察 LogCat 窗口下的提示信息,输出什么?为什么?
运行程序,输出结果如下图,因为广播接收者M y Broadcast ReceiverOne的优先级为1000,广播接收者M y Broadcast ReceiverTwo的优先级为200,
广播接收者M y Broadcast ReceiverThree的优先级为600,优先级高的先接收到。
问题2 :若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将M y Broadcast ReceiverTwo 注册在M y Broadcast ReceiverOne 前面, 再来运行 程序,观察结果,你能得出什么结论?
运行程序结果如下图,优先级相同的时候在清单文件中先注册的广播接收者先接收到广播。
问题3 :修改M y Broadcast ReceiverTwo 如下,观察结果,你又可以得出什么结论?
运行结果如下图,在广播接收者Two接收到广播之后调用了abortBroadcast()方法之后。广播被拦截了,所以广播不会往下继续传递,
广播接收者one和广播接收者Three则不会接收到广播。
四、实验运行结果图及实现代码
1.布局activity_main:
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/stitch_one" tools:context="bzu.edu.cn.orderedbroadcast.MainActivity"> android:id="@+id/btnFS" android:text="发送有序广播" android:onClick="send" android:textSize="16sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />
2.创建的第一个广播接收者MyBroadcastReceiverOne:
package bzu.edu.cn.orderedbroadcast;
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver { public MyBroadcastReceiverOne() { }
@Override public void onReceive(Context context, Intent intent) { Log.i("MyBroadcastReceiverOne","自定义的广播接收者one,接收到了广播事件"); } }
3.创建第二个广播接收者MyBroadcastReceiverTwo:
package bzu.edu.cn.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
public MyBroadcastReceiverTwo() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
abortBroadcast();//拦截有序广播
Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我拦截了");
}
}
4.创建第三个广播接收者MyBroadcastReceiverThree:
package bzu.edu.cn.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
public MyBroadcastReceiverThree() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");
}
}
5.MainActivity:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent=new Intent();
//定义广播的事件类型
intent.setAction("Intercept_Stitch");
//发送广播
sendOrderedBroadcast(intent,null);
}
}
6.配置文件AndroidManifest.xml:
package="bzu.edu.cn.orderedbroadcast">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:name=".MyBroadcastReceiverTwo"
android:enabled="true"
android:exported="true" >
android:name=".MyBroadcastReceiverOne"
android:enabled="true"
android:exported="true">
android:name=".MyBroadcastReceiverThree"
android:enabled="true"
android:exported="true">