有序广播案例实现

来源:互联网 发布:淘宝客优惠券网站 编辑:程序博客网 时间:2024/05/21 06:38

有序广播的小案例

通过设置不同的优先级查看广播接受者的接受顺序,或通过不同的注册顺序查看广播接受者的接受顺序
首先先写基本的布局文件和广播接收器
这里写图片描述
布局文件:

<?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:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:background="@drawable/stitch_one"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.bz0209.myapplication_3.MainActivity">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="80dp"        android:onClick="send"        android:text="发送有序广播"        android:paddingLeft="5dp"        android:paddingRight="5dp"        android:background="#FBFBFF"        android:textSize="20sp"        /></RelativeLayout>

Main_Activity:

package com.example.bz0209.myapplication_3;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);    }}

MyReceiverone(其他两个只是输出的不一样其他一样):

package com.example.bz0209.myapplication_3;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiverone extends BroadcastReceiver {    public MyReceiverone() {    }    @Override    public void onReceive(Context context, Intent intent) {        // TODO: This method is called when the BroadcastReceiver is receiving        // an Intent broadcast.        Log.i("one", "one 接收到了事件");    }}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.bz0209.myapplication_3">    <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=".MyReceiverone"            android:enabled="true"            android:exported="true">            <intent-filter android:priority="1000">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>        <receiver            android:name=".MyReceivertwo"            android:enabled="true"            android:exported="true" >            <intent-filter android:priority="200">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>        <receiver            android:name=".MyReceiverthree"            android:enabled="true"            android:exported="true">            <intent-filter android:priority="600">                <action android:name="Intercept_Stitch"/>            </intent-filter>        </receiver>    </application></manifest>

注意上面的Manifest文件中不同receiver的优先级
1. 正常运行测试:
这里写图片描述
我们通过日志的打印信息可以看到广播的接受是按照事先设定的优先级接收的。
2. 我们把MyReceivertwo的优先级同样设置成为1000并且把注册信息移动到MyReceiverone前面,运行效果如图:
这里写图片描述
我们发现优先级相同的广播接收者先被注册的先接收广播
3. 我们把MyReceivertwo修改为下面图片:
这里写图片描述
然后运行测试:
这里写图片描述
我们看到广播在MyReceivertwo被拦截,所以MyReceiverone和MyReceiverthree接收不到我广播广播

综上我们可以得出结论:

  • 广播的接受是按照事先设定的优先级接收的
  • 优先级相同的广播接收者先被注册的先接收广播
  • 如果广播在传递过程中被拦截,则后面的广播则无法接受广播
原创粉丝点击