【Android】广播(标准广播和有序广播)

Android中的广播大致有两种:有序广播和标准广播。标准广播是一种完全异步执行的广播,广播发出后,广播接收器几乎都会在同一时刻接收到这条广播消息,因此它们之间没有任何先后顺序而言,这种广播的执行效率较高,但是也因为这它是无法被截断的。有序广播是一种同步执行的广播,广播发出后,同一时刻只会有一个广播接收器接收到广播,当这个广播接收器中的逻辑执行完毕后才会继续传播消息。并且前面的广播可以截断正在传播的广播。

下面看一个标准广播的例子:

1.先建立一个接收类,要继承BroadcastReciver类:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
 
public class MyBroadcastReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "received in MyBroadcastReceiver",
				Toast.LENGTH_SHORT).show();
 
	}
 
}

2.配置AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" 
	android:versionCode="1" 
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />
	<application 
		android:icon="@drawable/icon" 
		android:label="@string/app_name">
		<activity 
			android:name=".Hello" 
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<receiver android:name="org.lxh.demo.MyBroadcastReceiver">
		    <intent-filter>
		        <action android:name="com.example.broadcast.MY_BROADCASTTEST"/>
		    </intent-filter>
		</receiver>	
		    </application>
	
</manifest>

/**<receiver android:name="org.lxh.demo.MyBroadcastReceiver">
		    <intent-filter>
		        <action android:name="com.example.broadcast.MY_BROADCASTTEST"/>
		    </intent-filter>
		</receiver>*/

3。发送程序:

package org.lxh.demo;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class Hello extends Activity {
	private Button btn=null;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		this.btn=(Button)super.findViewById(R.id.mybtn);
		this.btn.setOnClickListener(new ClickListenerImpl());
 
	}
	private class ClickListenerImpl implements OnClickListener{
 
		public void onClick(View arg0) {
			Intent intent=new Intent("com.example.broadcast.MY_BROADCASTTEST");
			sendBroadcast(intent);
			
		}
		
	}

在这里插入图片描述
下面再建立一个广播接收类:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
 
public class AnotherBroadcastReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "received in AnotherBroadcastReceiver",
				Toast.LENGTH_SHORT).show();
 
	}
 
}

再配置一次AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest 
	xmlns:android="http://schemas.android.com/apk/res/android"
	package="org.lxh.demo" 
	android:versionCode="1" 
	android:versionName="1.0">
	<uses-sdk android:minSdkVersion="10" />
	<application 
		android:icon="@drawable/icon" 
		android:label="@string/app_name">
		<activity 
			android:name=".Hello" 
			android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<receiver android:name="org.lxh.demo.MyBroadcastReceiver">
		    <intent-filter>
		        <action android:name="com.example.broadcast.MY_BROADCASTTEST"/>
		    </intent-filter>
		</receiver>	
		<receiver android:name="org.lxh.demo.AnotherBroadcastReceiver">
		    <intent-filter>
		        <action android:name="com.example.broadcast.MY_BROADCASTTEST"/>
		    </intent-filter>
		</receiver>	
		    </application>
	
</manifest>

两个广播接收器过滤同一个广播,所以他们会同时收到此广播,运行如下:
在这里插入图片描述
在这里插入图片描述

即先显示第一个注册的广播,后显示第二个。我们考虑如何改变接收的顺序?可以设置优先级:

<receiver android:name="org.lxh.demo.MyBroadcastReceiver">
		    <intent-filter>
		        <action android:name="com.example.broadcast.MY_BROADCASTTEST"/>
		    </intent-filter>
		</receiver>	
		<receiver android:name="org.lxh.demo.AnotherBroadcastReceiver">
		    <intent-filter android:priority="100">
		        <action android:name="com.example.broadcast.MY_BROADCASTTEST"/>
		    </intent-filter>
		</receiver>

此时运行接收顺序就反过来了。下面考虑如何不让第二个接收器接收到广播?我们可以再第一个接收器内写入abortBroadcast();即:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
 
public class AnotherBroadcastReceiver extends BroadcastReceiver {
 
	@Override
	public void onReceive(Context context, Intent intent) {
		Toast.makeText(context, "received in AnotherBroadcastReceiver",
				Toast.LENGTH_SHORT).show();
		abortBroadcast();
 
	}
 
}

报错:E/BroadcastReceiver(435): BroadcastReceiver trying to return result during a non-ordered broadcast: E/BroadcastReceiver(435): java.lang.RuntimeException: BroadcastReceiver trying to return result during a non-ordered broadcast
提示这是一个没有顺序的广播。改正只需将发送广播的程序改为sendOrderedBroadcast(intent, null);:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
 
public class Hello extends Activity {
	private Button btn=null;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState); // 生命周期方法
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.main); // 设置要使用的布局管理器
		this.btn=(Button)super.findViewById(R.id.mybtn);
		this.btn.setOnClickListener(new ClickListenerImpl());
 
	}
	private class ClickListenerImpl implements OnClickListener{
 
		public void onClick(View arg0) {
			Intent intent=new Intent("com.example.broadcast.MY_BROADCASTTEST");
			//sendBroadcast(intent);
			sendOrderedBroadcast(intent, null);
			
		}
		
	}
}

此时再运行实例,只会显示第一条Toast:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值