发送有序广播

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

本次案例要实现的功能是拦截一条有序广播。通过send()OrderedBroadcast()方法发送一条有序广播。
并运行程序:
1:程序启动后,单击“发送有序广播”按钮,发出一条广播事件,此时观察LogCat窗口下的提示信息
2:若将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面,再来运行程序
3:修改MyBroadcastReceiverTwo如下
这里写图片描述

最终要得到的运行结果如下图:
这里写图片描述

1.首先在目录app/res/drawable中粘贴图片stitch_one.png作为背景图。
2.创建布局文件activity_main.xml,内容如下:

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.oderedbroadcast.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>

3.编写界面交互代码(MainActivity.java),代码如下:

package com.example.administrator.oderedbroadcast;

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);
    }
}

4.创建广播接收者,具体步骤如下图:
这里写图片描述

MyBroadcastReceiverOne中的代码如下:

package com.example.administrator.oderedbroadcast;

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) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("MyBroadcastReceiverOne","自定义的广播接收者One,接收到了广播事件");
    }
}

5.配置清单文件AndroidManifest.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.oderedbroadcast">

    <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="1000">
                <action android:name="Intercept_Stitch" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

运行结果如下图:
这里写图片描述
窗口下的提示信息如下图:
这里写图片描述

6.再次创建广播接收者MyBroadcastReceiverTwo,代码如下:

package com.example.administrator.oderedbroadcast;

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) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
    }
}

要将广播接收者MyBroadcastReceiverTwo优先级同样设置为1000,并将MyBroadcastReceiverTwo注册在MyBroadcastReceiverOne前面,则修改清单文件AndroidManifest.xml,内容为:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.oderedbroadcast">

    <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=".MyBroadcastReceiverTwo"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="Intercept_Stitch" />
            </intent-filter>
        </receiver
        <receiver
            android:name=".MyBroadcastReceiverOne"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">
                <action android:name="Intercept_Stitch" />
            </intent-filter>
        </receiver>
>
    </application>
</manifest>

运行后,窗口提示词如下图:
这里写图片描述

7.修改MyBroadcastReceiverTwo之后,代码如下:

package com.example.administrator.oderedbroadcast;

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) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Log.i("MyBroadcastReceiverTwo","自定义的广播接收者Two,接收到了广播事件");
        abortBroadcast();//拦截有序广播
        Log.i("MyBroadcastReceiverTwo","我是广播接收者Two,广播被我终结了");
    }
}

运行后,窗口提示词如下图:
这里写图片描述
结论:优先级最高的最先收到广播事件,说明广播接收者的优先级决定广播接收的顺序。高优先级的广播是可以被终止的,在MyBroadcastReceiverTwo添加abortBroadcast()拦截广播。

Android 发送有序广播 这次案例运行的效果就是点击下面运行图上的发送有序广播,会有在Android Studio上出现广播接收的提示信息。 这是此次案例的布局图: 1、首先要根据要求进行布局,具体的布局的代码如下: xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.andr 阅读详情

相关推荐

Android广播(接收,发送,标准,有序,全局,本地,实战,填坑)

Android 广播一、广播是什么?二、广播分类1.标准广播2.有序广播三、广播的使用(接收器)1.广播的注册1.1动态注册实例1.2 静态注册实例 一、广播是什么? 通俗点李姐,广播就是安卓系统本身发出的声音,我们可以通过安卓提供给我们的一系列内容来接收和发出广播,以此来简单快捷地实现一些功能。 二、广播分类 1.标准广播 标准广播是完全异步执行(异步同步详情见百度,概念很好李姐)同时不可被截断,也就是说当前发出的一号广播,会被ABC三个接收器同时接收到。如下图 2.有序广播 顾名思义,一种有序的,同.

qq_43667625的博客 1万+

Android组件(三)Broadcast

BroadcastReceiver broadcastreceiver 1.简述Broadcast 2.广播 2.1 标准广播(Normal broadcasts) 2.2 有序广播(Ordered broadcasts) 2.3 粘性广播(Sticky Broadcast) 3.动态注册 3.1 什么是动态注册 3.2 动态注册步骤 3.3 动态注册监听网络变化 4.静态注册 3.1 什么是静态注册 3.2 静态注册实现开机启动 4.发送标准广播 5.发送有序广播 6.本地广播

Rpk712的博客 2348

Android学习笔记之发送有序广播和标准广播

Android学习笔记 参考《第一行代码 第二版》 关于广播:即系统之间相互传递消息 有了接收者自然也存在发送者。即广播发送广播发送的两种方法 发送标准广播 标准广播即给需要接收该广播类型的,所有广播接收者,进行发送,他们即广播接收者,几乎同时收到广播发送与自定义广播发送相同这里就不在赘述。 发送有序广播 有序广播即对接收广播广播接收者进行先后顺序的区分,让他们有序...

Xuan_Jackson的博客 1182

Broadcast发送有序广播

这里一般和有序代码全部罗列出来 但本章侧重于关注有序广播发送与接收内容 发送端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"

we1less的博客 471

发送自定义广播有序广播

之前章节介绍了如何发送和接收标准广播。标准广播发送后,可以被多个广播接收器同时接收到。除了标准广播,还有有序广播。对于有序广播广播接收器接收广播是有先后顺序的,而且前面的广播接收器还可以将广播截断,阻止其继续传播。

“弈鸣” 的 互联网技术博客 1747

Android中发送有序广播案例

Android系统提供了两种广播类型,一种是有序广播,一种是有序广播。 (1)无序广播是完全异步执行,发送广播时所有监听这个广播广播接收者都会收到此消息,但接收的顺序不确定。 (2)有序广播是按照接收者的优先级接收,只有一个广播接收者能接收信息,在此广播接收者中逻辑执行完毕后,才会继续传递。 实验要求:通过sendOrderedBroadeast()发送一条有序广播

小白一只 2929

51.发送有序广播

• 从 Android 8.0 起(API 26),系统对隐式广播做了限制。如果使用清单注册(manifest-declared)的广播接收器接收自定义隐式广播,可能收不到广播下面示例采用第二种方法,在 MainActivity 中发送有序广播前通过 setPackage() 指定包名,从而确保广播送达到清单中注册的接收器。下面提供完整的教学示例,演示如何发送有序广播,并详细说明每部分的作用和注意事项。在main.xml中。

2303_79132118的博客 399

Android——发送广播有序广播

广播是一种可以跨进程的通信方式,这一点从前面接收系统广播的时候就可以看出来 了。因此在我们应用程序内发出的广播,其他的应用程序应该也是可以收到的。 1.我们需要再新建一个 BroadcastTest2项目。 将项目创建好之后,还需要在这个项目下定义一个广播接收器,用于接收上一小节中的 自定义广播。新建 AnotherBroadcastReceiver继承自 BroadcastReceiver,代码...

qq_41430142的博客 3330

android-BroadcastReceiver 发送有序广播

普通广播(Normal Broadcast): 一,优缺点:和有序广播的优缺点相反! 二,发送广播方法sendBroadcast() 有序广播(Ordered Broadcast): 一,优缺点 优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver              2,通过abortBroadcast可终止广播的传播

superjunjin的专栏 1万+

发送有序广播——BroadcastReceiver

有序广播(Ordered Broadcast): 一,优缺点 优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver       2,通过abortBroadcast可终止广播的传播   缺点:效率低   二,发送广播方法:sendOrderedBroadcast()    三,

ly1012053441的博客 665
上一篇: 网络图片查看器
下一篇: 电话拦截器
wl1fy
博客等级 码龄10年 0粉丝 10原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值