实验目标:掌握有序广播的使用*
1:设计布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:background="@drawable/stitch_one"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.edu.bzu.myapplication.MainActivity">
<Button
android:text="发送有序广播"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:paddingLeft="5dp"
android:onClick="send"
android:textSize="20sp"
android:background="#FBFBFF"
android:paddingRight="5dp"
android:id="@+id/button" />
</RelativeLayout>
2:编写界面交互代码
点击事件处理方法:
package com.edu.bzu.myapplication;
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");
//发送广播
sendBroadcast(intent,null);
}
}
3:修改权限并注册广播接受者
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edu.bzu.myapplication">
<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=".MybroadcastReceiverOne"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100"/>
</receiver>
<receiver android:name=".MybroadcastReceiverOne">
<intent-filter android:priority="1000" >
<action android:name="Intercept_Stitch"/>
</intent-filter>
<!--<receiver android:name=".MybroadcastReceiverTwo">-->
<!--<intent-filter android:priority="200" >-->
<!--<action android:name="Intercept_Stitch"/>-->
<!--</intent-filter>-->
<!--<receiver android:name=".MybroadcastReceiverThree">-->
<!--<intent-filter android:priority="600" >-->
<!--<action android:name="Intercept_Stitch"/>-->
<!--</intent-filter>-->
<!--</receiver>-->
</application>
</manifest>
4:创建广播接受者MybroadcastReceiverOne
package com.edu.bzu.myapplication;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MybroadcastReceiverOne extends BroadcastReceiver {
public MybroadcastReceiverOne() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MybroadcastReceiverOne","自定义的广播接受者One,接收到了广播事件");
abortBroadcast();//
Log.i("MybroadcastReceiverTwo","我是广播接受者Two,广播被我终结了!");
}
}
5:运行结果显示: