广播的类型————拦截有序广播
来源:互联网 发布:脱发知乎 编辑:程序博客网 时间:2024/06/01 10:30
导语: 在Android系统中,根据广播的执行顺序不同,可将其分为有序广播和无序广播。有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够接收到这条消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以,此时的广播接收器是有先后顺序的,并且可以被拦截。有序广播发送一条消息后,高优先级的广播接收器先接收到广播,低优先级的广播后接收到广播。想要拦截一条广播,可使用abortBroadcast();方法。
下面具体介绍如何实现拦截有序广播
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:background="@drawable/stitch_one"
tools:context="com.example.bz0209.orderedbroadcast.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>
2.界面交互代码的设计与实现
package com.example.bz0209.orderedbroadcast;
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_Switch");
sendOrderedBroadcast(intent,null);
}
}
3.创建三个广播接收者:
<1>MyBroadcastReceiverOne.java
package com.example.bz0209.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件");
}
}
<2>MyBroadcastReceiverTwo.java
package com.example.bz0209.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
abortBroadcast();//拦截有序广播
Log.i("MyBroadcastReceiverTwo","我是接收者Two,广播被我终结了");
}
}
<3>MyBroadcastReceiverThree.java
package com.example.bz0209.orderedbroadcast;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
@SuppressLint("LongLogTag")
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverThree","自定义的广播接收者Three,接收到了广播事件");
}
}
4.设置优先级广播接收者的优先级
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bz0209.orderedbroadcast">
<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>
<receiver android:name=".MyBroadcastReceiverOne">
<intenf-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intenf-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverTwo">
<intenf-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intenf-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverThree">
<intenf-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intenf-filter>
</receiver>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100"/>
</receiver>
</activity>
</application>
</manifest>
其中在清单文件中注册广播接收者<intent-filter android:priority="100"/>属性指定数值越大,优先级越高。如果两个广播接收者的优先级相同,则先注册的组件优先接收到广播。如果两个应用程序监听了同一个广播事件并设置了优先级,则先安装的应用优先接收到广播。
5.运行效果如下
- 广播的类型————拦截有序广播
- 广播接收者——案例:拦截有序广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android四大组件——BroadcastReceiver普通广播、有序广播、拦截广播、本地广播、Sticky广播、系统广播
- Android之广播机制—有序广播
- Android--广播的类型及拦截有序广播案例
- 广播——BroadcastReceiver的有序广播和优先级
- 案例——有序广播
- 广播——有序广播优先级相同情况测试
- Android——有序广播和无序广播
- 广播--案例:拦截有序广播
- Android--有序广播的实例解析--案例《拦截有序广播》
- 有序广播的实例解析--android案例《拦截有序广播》
- 发送有序广播——BroadcastReceiver
- Android菜鸟的成长笔记(26)——普通广播与有序广播
- 安卓实验课程——有序广播的实现。
- 对于Http的编码统一
- python中的dir()和__dict__
- 批量操作之多选
- bootanimation.zip 替换/更换 黑屏
- 策略模式Strategy(对象行为型)
- 广播的类型————拦截有序广播
- AlertDialog弹框apk版本升级
- 【LeetCode】 73. Set Matrix Zeroes
- Mysql 创建数据库、用户、授权、导入数据
- mysql百万级数据优化
- 移动端图片轮播?
- NumPy 便利的函数
- optee os 的启动和初始化
- android studio 插件版本太旧导致不能正常编译