Android 不归路之广播BoradCast篇

本文详细介绍了Android广播的工作原理,包括普通广播和有序广播的区别。普通广播的接收顺序不确定,而有序广播可以通过设置优先级控制接收顺序。有序广播还可以传递数据和终止广播。在实际应用中,错误地设置优先级和使用广播方法可能导致异常。理解这些概念对于优化Android应用的广播处理至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 广播能在一个Activity 里发送广播消息,所有“已打开”的Activity 会接收到该“频段”(action)的消息 执行操作

广播理论上都是异步的,普通广播 通过Context.sendBroadcast()方法发送,receive接收广播的顺寻不确定,有序广播通过Context.sendOrderedBroadcast()方法发送消息,并通过filter.setpriority()【android-priority】 设置优先级顺序。优先级相同,顺序不确定的时候按照BroadcastReceiver注册的先后顺序获得广播。有序广播可以终止广播继续发送,方法为:BroadcastReceiver.abortBroadcast()。对于有序广播,前面的接收者可以将数据通过setResultExtras(Bundle)方法存放进结果对象,然后传给下一个接收者,下一个接收者通过代码:Bundle bundle = getResultExtras(true));获取上一个接收者存入在结果对象中的数据。

注册广播接收,这样就可以接受同一action值的广播

<span style="white-space:pre">	</span>//处理广播
<span style="white-space:pre">	</span>private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
<span style="white-space:pre">		</span>@Override
<span style="white-space:pre">		</span>public void onReceive(Context context, Intent intent) {
<span style="white-space:pre">			</span>String action = intent.getAction();
<span style="white-space:pre">			</span>if (action.equals(ACTION_NAME)) {
<span style="white-space:pre">				</span>Bundle bundle = new Bundle();  
<span style="white-space:pre">			</span>      bundle.putString("first", "第一个BroadcastReceiver存入的消息");  
<span style="white-space:pre">				</span>setResultExtras(bundle);
<span style="white-space:pre">				</span>Toast.makeText( context,intent.getStringExtra("msg"),1000).show();

<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}


<span style="white-space:pre">	</span>};
<span style="white-space:pre">	</span>//注册
<span style="white-space:pre">	</span>public void registerBoradcastReceiver() {
<span style="white-space:pre">		</span>//拦截器
<span style="white-space:pre">		</span>IntentFilter myIntentFilter = new IntentFilter(ACTION_NAME);
<span style="white-space:pre">		</span>myIntentFilter.setPriority(10);
<span style="white-space:pre">		</span>// 注册广播
<span style="white-space:pre">		</span>registerReceiver(mBroadcastReceiver, myIntentFilter);
<span style="white-space:pre">	</span>}
发送广播

<span style="white-space:pre">	</span>Intent intent = new Intent(ACTION_NAME);
	intent.putExtra("msg","6666666");
	// 发送广播
	sendOrderedBroadcast(intent,null);
详细代码:
package com.ht.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.example.hello.R;

public class MyBroadCast extends Activity {
	private static final String ACTION_NAME = "发送广播";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 注册广播接收
		registerBoradcastReceiver();
		setContentView(R.layout.broadcast_test);
		Button b1 = (Button) findViewById(R.id.button1);
		b1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(ACTION_NAME);
				intent.putExtra("msg","6666666");
				// 发送广播
				<span style="color:#ff0000;">sendOrderedBroadcast(intent,null);</span>
			}
		});
	}
	//处理广播
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(ACTION_NAME)) {
			<span style="white-space:pre">	</span>Bundle bundle = new Bundle();  
			        bundle.putString("first", "第一个BroadcastReceiver存入的消息");  
				setResultExtras(bundle);
			<span style="white-space:pre">	</span>Toast.makeText(context,intent.getStringExtra("msg"),1000).show();  

			}
		}

	};
	//注册
	public void registerBoradcastReceiver() {
		//拦截器
		IntentFilter myIntentFilter = new IntentFilter(ACTION_NAME);
		myIntentFilter.setPriority(10);//优先级
		// 注册广播接收
		registerReceiver(mBroadcastReceiver, myIntentFilter);
	}

    @Override
    protected void onDestroy() {
    	//反注册,不再接收
    	unregisterReceiver(mBroadcastReceiver);
    	super.onDestroy();
    }
}
在写demo的时候报了
BroadcastReceiver trying to return result during a non-ordered broadcast异常

原因在于:设置了优先级 却没有用sendOrderedBroadcast();
而是用了sendBroadcast()方法发送广播。

sendBroadcast()这个方法的广播是能够发送给所有广播接收者,按照注册的先后顺序,如果这个时候设置了广播接收者的优先级,优先级如果恰好与注册顺序相同,则不会有任何问题,如果顺序不一样,会出leaked IntentReceiver 这样的异常,并且在前面的广播接收者不能调用abortBroadcast()方法将其终止,如果调用会出BroadcastReceiver trying to return result during a non-ordered broadcast的异常

priority的值在(-1000,1000)之间,事实上,当 priority 值为integer 的最大值才时,才是优先级最高的,即  2147483647。此时可以拦截系统短信进入收件箱,也不会有提示音。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值