android-BroadcastReceiver 发送有序广播

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

普通广播(Normal Broadcast):

一,优缺点:和有序广播的优缺点相反!

二,发送广播的方法:sendBroadcast()


有序广播(Ordered Broadcast):

一,优缺点

优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver

             2,通过abortBroadcast可终止广播的传播  

缺点:效率低  

二,发送广播的方法:sendOrderedBroadcast()   

三,优先接收到Broadcast的Receiver可通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,

下一个Receiver 通过 Bundle bundle=getResultExtras(true)方法获取上一个 Receiver传来的数据     

 

程序效果:点击按钮,两个Receiver接收同一条广播,在logcat中打印出数据(按照Receiver的优先顺序,Receiver2先,Receiver1后      

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.song"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".C48_BroadcastActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!--优先级的设定 MyReceiver2大于MyReceiver1,优先级的范围-1000~1000 -->
        </activity>
        <receiver android:name=".MyReceiver1">
            <intent-filter android:priority="200">
                <action android:name="com.song.123"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".MyReceiver2">
            <intent-filter android:priority="1000">
                <action android:name="com.song.123"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

主Activity

package com.song;
//发送广播,bundle绑上key为a的数据
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class C48_BroadcastActivity extends Activity {
    /** Called when the activity is first created. */
	Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent("com.song.123");
				Bundle bundle=new Bundle();
				bundle.putString("a", "aaa");
				intent.putExtras(bundle);
				//有序广播
				sendOrderedBroadcast(intent, null);
			}
		});
        
    }
}

Receiver2
package com.song;
//优先接到广播,bundle绑上key为b的数据
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class MyReceiver2 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("receiver2");
//		context.getSystemService(name);
		Bundle bundle=intent.getExtras();
		bundle.putString("b", "bbb");
		System.out.println("a="+bundle.get("a"));
		setResultExtras(bundle);
		//切断广播
//		abortBroadcast();
	}

}


Receiver1

package com.song;
//接收从receiver2传来的广播,包含key为a和b的数据
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

public class MyReceiver1 extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		System.out.println("receiver1");
		//要不要接受上一个广播接收器receiver2传来的的数据
		Bundle bundle=getResultExtras(true);
		System.out.println("a="+bundle.getString("a")+",b="+bundle.getString("b"));
	}

}


程序效果


       

深入理解Android广播机制及源码分析 本文还有配套的精品资源,点击获取 简介:Android广播机制是应用程序异步通信的关键组件,涉及多种类型的广播及其工作原理。本文详细介绍有序广播、无序广播、本地广播、开机广播、应用安装卸载广播、SD卡挂载广播、锁屏开屏广播自定义广播等,并解释如何在AndroidManifest.xml中注册BroadcastReceiver,创建BroadcastReceiver类以及如... 阅读详情

相关推荐

Broadcast组件——收发广播应用——收发有序广播

布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"...

小白龙白龙马的博客 214

有序广播详解

有序广播详解前言概述有序广播注意项动态注册 接收有序广播静态注册 接收有序广播Android8.0后不支持)发送有序广播特殊的广播接收者 前言 Google从Android8.0版本开始,对在清单文件中静态注册广播做了限制。 概述 类似于中央下发的红头文件,一级一级向下发送,中间如果接收失败,发送就会终止。 发送过程中,可以终止。终止后,后续的接收者则无法接收(有一种特殊情况)。 有序广播注意项...

凉拌西红柿 1394

安卓广播机制讲解(标准广播有序广播)

安卓广播机制讲解 广播分为标准广播有序广播 标准广播(Normal broadcasts)是一种完全异步执行的广播,在广播发出之后,所有的广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序可言。这种广播的效率会比较高,但同时也意味着它是无法被截断的。标准广播的工作流程如图所示。 有序广播(Ordered broadcasts )则是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够收到这条广播消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以此时

天行健 君子以自强不息,地势坤 君子以厚德载物。 5114

Android中的有序广播与无序广播

本文我们介绍Android中无序广播有序广播。 无序广播,又叫普通广播。所有的接收器几乎同时收到广播,无先后顺序。 有序广播发送广播会按优先级顺序被接收。同一时刻只会有一个接收器收到广播,且可以对广播截断,修改。

fxjzzyo的博客 3460

Android广播机制

Android广播机制 广播机制简介 Android提供了一套完整的API,允许应用程序自由的发送接受广播发送广播借助于我们之前学过的:Intent,而接收广播需要借助于广播接收器(Broadcast Receiver) 广播的类型主要分为两种:标准广播有序广播。 标准广播:一种完全异步执行的广播,在广播发出之后,所有接收器几乎在同一时刻接收到这条广播消息,因此它们之间没有任何的先后顺序可...

low666的博客 832

安卓BroadcastReceiver组件使用系列2:给多个广播接收发送广播有序广播的使用

给多个广播接收发送广播有序广播的使用在安卓开发中是经常使用的方式,下面我们来介绍一下它的使用方法。 整体思路:在xml文件中放置两个Button控件,给这两个Button控件设置点击事件,在第一个点击事件中传递一个数据,设置一个动作并发送广播,在第二个点击事件中传递一个数据,设置一个动作并发送有序广播。新建三个继承BroadcastReceiver广播接收类,用于接收传递的数据。在第二个B

luoshiwutai的博客 2731

Android的Service与BroadcastReceiver之四 BroadcastReceiver 简介、开发与配置;发送广播有序广播;使用BroadcastReceiver接受系统广播

BroadcastReceiver 简介、开发与配置 本质上就是一种全局监听器,用于监听全局的广播消息。它可以非常方便地实现系统中不同组件之间的通信 BroadcastReceiver用于接收程序锁发生的Broadcast Intent,启动BroadcastReceiver只需要两步 创建需要启动的BroadcastReceiver的Intent调用Context的sendBroa

编程问题与思索 687

Android BroadcastReceiver 发送有序广播

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

weixin_34357267的博客 154

Android广播BroadcastReceiver接收系统广播(动态、静态注册方式)、发送自定义广播发送有序广播发送标准广播)、BroadcastReceiver实践——强制下线功能

广播BroadcastReceiver接收系统广播(动态、静态注册方式)、发送自定义广播发送标准广播发送有序广播)、BroadcastReceiver实践——强制下线功能

摸鱼小小虫的博客 3535

Android BroadcastReceiver实例Demo(有序广播发送

上一篇简单介绍了广播发送,这篇主要介绍下,有序广播发送。 设置完相关属性的时候,广播就会按照有序的方式进行发送发送顺序: 第一条广播; 再发送第二条广播; 最后发送第三条广播。 代码如下: 布局文件: activity_main(一个Button): <RelativeLayout xmlns:android="http://schemas.android

红日 1770

Android 四大组件之BroadcastReceiver发送有序无序广播

无序广播 发送 /**无序广播*/ Intent intent =new Intent(Broadcast); intent.putExtra("msg", "无序广播发送"); sendBroadcast(intent); 接收 public String Broadcast="com.sendbroadcast.action.BROADCAST"; //接收广

Rlingge的博客 1260

android注销广播报错,BroadcastReceiver

BroadcastReceiver 广播接受者Android中, Broadcast是一种数据传递的方式/通信方式.Brodadcast 是Android 系统组件之一广播的特性1. 由一个发送方, 若干接收方组成2. 发送方与接收方存在匹配规则调用方式Intent intent = new Intent(this, XXX.class);intent.setAction("xxx"); //...

weixin_31162247的博客 402

Android开发之BroadcastReceiver

BroadcastReceiver介绍

大编码时代的一介Coder 1142

Android入门:广播发送者与广播接收

一、广播发送者&广播接收者介绍 1.广播接收广播接收者简单地说就是接收广播意图的Java类,此Java类继承BroadcastReceiver类,重写: public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据; 广播意图就是通过Context.sendBroadcast(Int

xiazdong 19万+
上一篇: android-BroadcastReceiver 发送一条普通广播
下一篇: android-BroadcastReceiver 通过终止广播 阻止用户收到短信
superjunjin
博客等级 码龄17年 600粉丝 133原创
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值