博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
广播发送者&广播接收者介绍
阅读量:6009 次
发布时间:2019-06-20

本文共 8004 字,大约阅读时间需要 26 分钟。

hot3.png

1.广播接收者

广播接收者简单地说就是接收广播意图的Java类,此Java类继承BroadcastReceiver类,重写:

public void onReceive(Context context,Intent intent),其中intent可以获得传递的数据;

广播意图就是通过Context.sendBroadcast(Intent intent)或Context.sendOrderedBroadcast(Intent intent)发送的意图,通过这个语句,能够广播给所有满足条件的组件,比如intent设置了action="com.xiazdong",则所有在AndroidManifest.xml中设置过<action android:name="com.xiazdong"/>的广播接收者都能够接收到广播;

:onReceive方法必须在10秒内完成,如果没有完成,则抛“Application No Response”当广播接收者onReceive方法需要执行很长时间时,最好将此耗时工作通过Intent发送给Service由Service完成,并且不能使用子线程解决,因为BroadcastReceiver是接收到广播后才创建的,并且生命周期很短,因此子线程可能在没有执行完就已经被杀死了。

[java]
  1. public void onReceive(Context context,Intent intent){
  2. Intent intent = new Intent(context,XxxService.class);
  3. context.startService(intent);
  4. }
public void onReceive(Context context,Intent intent){	Intent intent = new Intent(context,XxxService.class);	context.startService(intent);}

2.广播发送者

通常广播发送方就是调用Context.sendBroadcast()的程序,而广播接收者就是继承BroadcastReceiver的程序;

通常广播发送方都是通过隐式意图,这样才能发送给多人;

广播发送方分为普通广播和有序广播;

同步广播:发送方发出后,几乎同时到达多个广播接收者处,某个接收者不能接收到广播后进行一番处理后传给下一个接收者,并且无法终止广播继续传播;Context.sendBroadcast(intent);

有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的<intent-filter android:priority="xxx">设置;比如存在3个广播接收者A、B、C,优先级A>B>C,因此A最先收到广播,当A收到广播后,可以向广播中添加一些数据给下一个接收者(intent.putExtra()),或者终止广播(abortBroadcast());Context.sendOrderedBroadcast(intent);

二、广播接收者核心代码

同步广播发送方核心代码:

[java]
  1. Intent intent = new Intent();
  2. intent.setAction("...");
  3. Context.sendBroadcast(intent);
Intent intent = new Intent();intent.setAction("...");Context.sendBroadcast(intent);

有序广播发送方核心代码:

[java]
  1. Intent intent = new Intent();
  2. intent.setAction("...");
  3. Context.sendOrderedBroadcast(intent,null);
Intent intent = new Intent();intent.setAction("...");Context.sendOrderedBroadcast(intent,null);

广播接收者核心代码:

[java]
  1. public class Receiver extends BroadcastReceiver{
  2. public void onReceive(Context context, Intent intent) {
  3. Bundle bundle = intent.getExtras();
  4. ...
  5. }
  6. }
public class Receiver extends BroadcastReceiver{	public void onReceive(Context context, Intent intent) {		Bundle bundle = intent.getExtras();		...	}}

AndroidManifest.xml

[html]
  1. <application>
  2. <receiver android:name=".Receiver">
  3. <intent-filter android:priority="1000">
  4. <action android:name="com.xiazdong"/>
  5. </intent-filter>
  6. </receiver>
  7. </application>

三、广播实例

1.同步广播实例

场景说明:

(1)广播发送者:

[java]
  1. package com.xiazdong.broadcastsender;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9. public class MainActivity extends Activity {
  10. private Button button;
  11. private OnClickListener listener = new OnClickListener(){
  12. @Override
  13. public void onClick(View v) {
  14. Intent intent = new Intent();
  15. intent.setAction("com.xiazdong");
  16. intent.putExtra("name", "xiazdong");
  17. MainActivity.this.sendBroadcast(intent);
  18. Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();
  19. }
  20. };
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. button = (Button)this.findViewById(R.id.button);
  26. button.setOnClickListener(listener);
  27. }
  28. }
package com.xiazdong.broadcastsender;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;import android.widget.Toast;public class MainActivity extends Activity {	private Button button;	private OnClickListener listener = new OnClickListener(){		@Override		public void onClick(View v) {			Intent intent = new Intent();			intent.setAction("com.xiazdong");			intent.putExtra("name", "xiazdong");			MainActivity.this.sendBroadcast(intent);			Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();		}	};	@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(listener);    }}

(2)广播接收者

[java]
  1. package com.xiazdong.broadcastreceiver1;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class Receiver extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. String name = intent.getExtras().getString("name");
  10. Log.i("Recevier1", "接收到:"+name);
  11. }
  12. }
package com.xiazdong.broadcastreceiver1;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class Receiver extends BroadcastReceiver {	@Override	public void onReceive(Context context, Intent intent) {		String name = intent.getExtras().getString("name");		Log.i("Recevier1", "接收到:"+name);	}}

AndroidManifest.xml

[java]
  1. <receiver android:name=".Receiver">
  2. <intent-filter>
  3. <action android:name="com.xiazdong"/>
  4. </intent-filter>
  5. </receiver>

结果:

2.有序广播实例

场景说明:

(1)广播发送者

[java]
  1. package com.xiazdong.broadcastsender;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9. public class MainActivity extends Activity {
  10. private Button button;
  11. private OnClickListener listener = new OnClickListener(){
  12. @Override
  13. public void onClick(View v) {
  14. Intent intent = new Intent();
  15. intent.setAction("com.xiazdong");
  16. intent.putExtra("name", "xiazdong");
  17. MainActivity.this.sendOrderedBroadcast(intent, null); //有序广播发送
  18. Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();
  19. }
  20. };
  21. @Override
  22. public void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.main);
  25. button = (Button)this.findViewById(R.id.button);
  26. button.setOnClickListener(listener);
  27. }
  28. }
package com.xiazdong.broadcastsender;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;import android.widget.Toast;public class MainActivity extends Activity {	private Button button;	private OnClickListener listener = new OnClickListener(){		@Override		public void onClick(View v) {			Intent intent = new Intent();			intent.setAction("com.xiazdong");			intent.putExtra("name", "xiazdong");			MainActivity.this.sendOrderedBroadcast(intent, null);	//有序广播发送			Toast.makeText(getApplicationContext(), "发送广播成功", Toast.LENGTH_SHORT).show();		}	};	@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(listener);    }}

(2)广播接收者

Receiver1

[java]
  1. package com.xiazdong.broadcastreceiver1;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class Receiver extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. String name = intent.getExtras().getString("name");
  10. Log.i("Recevier1", "接收到:"+name);
  11. abortBroadcast(); //Receiver1接收到广播后中断广播
  12. }
  13. }
package com.xiazdong.broadcastreceiver1;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class Receiver extends BroadcastReceiver {	@Override	public void onReceive(Context context, Intent intent) {		String name = intent.getExtras().getString("name");		Log.i("Recevier1", "接收到:"+name);		abortBroadcast();	//Receiver1接收到广播后中断广播	}}

AndroidManifest.xml

[html]
  1. <receiver android:name=".Receiver">
  2. <intent-filter android:priority="1000"> <!-- 设置最高优先级 -->
  3. <action android:name="com.xiazdong"/>
  4. </intent-filter>
  5. </receiver>

转载于:https://my.oschina.net/yuanxulong/blog/338090

你可能感兴趣的文章
年终奖
查看>>
相对/绝对路径,cd命令,mkdir/rmdir命令,rm命令
查看>>
OpenVAS in Metasploit
查看>>
TCl介绍
查看>>
repeater的几种用法
查看>>
java 静态块 代码块 静态变量 实例变量 构造
查看>>
【HTML5】DOMContentLoaded事件
查看>>
数据库之mysql(数据库编码问题)
查看>>
tomcat中web.xml各配置项的意义
查看>>
Linux下ftp+ssl实现ftps
查看>>
我的友情链接
查看>>
Java基础 - 第一章 计算
查看>>
CentOS7添加用户账户,授权
查看>>
金蝶kis记账王凭证过账要不要要审核
查看>>
Nodejs学习笔记(二):《node.js开发指南》代码中需要注意的几点
查看>>
Nginx 配置 SSL 及 SSL 证书的创建过程
查看>>
Ztree异步加载自动展开节点
查看>>
应用程序内购买类型
查看>>
大型企业网络构建(二)——高级OSPF&虚链路应用
查看>>
文件系统概念深入介绍及压缩归档工具
查看>>