BroadcastReceiver用于监听系统全局广播消息,由于BroadcastReceiver是一种全局的监听器,因此它可以非常方便地实现系统中不同组件之间通信

启动它需要两步

1.创建需要启动的BroadcastReceiver的Intent

2.调用Context的SendBroadcast或sendOrederedBroadcast方法来启动指定的BroadcastReceiver

这其中关键是创建Intent时,要setAction("xxx"),而BroadcastReceiver就要创建它的filter,或在代码中指定,或在xml中指定

下面以一个简单例子说明,例子是一个向广播发送消息的程序

发送端主界面代码

  1. package WangLi.Service.Broadcast;
  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. public class Broadcast extends Activity {
  9. Button send;
  10. @Override
  11. public void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. // 获取界面中的按钮
  15. send = (Button) findViewById(R.id.send);
  16. send.setOnClickListener(new OnClickListener() {
  17. @Override
  18. public void onClick(View arg0) {
  19. // 创建Intent对象
  20. Intent intent = new Intent();
  21. // 设置Intent的Action属性
  22. intent.setAction("WangLi.Test");
  23. intent.putExtra("msg", "简单的消息");
  24. //发送广播
  25. sendBroadcast(intent);
  26. }
  27. });
  28. }
  29. }

接着创建一个BroadcastReceiver监听器

  1. package WangLi.Service.Broadcast;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.widget.Toast;
  6. public class MyReceiver extends BroadcastReceiver {
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. Toast.makeText(
  10. context,
  11. "接收到的Intent的Action为:" + intent.getAction() + "\n消息内容是:"
  12. + intent.getStringExtra("msg"), 5000).show();
  13. }
  14. }

向AndroidManifest.xml中配置该receiver,主要是intent filter

  1. <receiver android:name="MyReceiver" >
  2. <intent-filter>
  3. <action android:name="WangLi.Test" />
  4. </intent-filter>
  5. </receiver>

要注意的是BroadcastReceiver的过期时间为10秒,如果是耗时的操作应放在service中去完成,BroadcastReceiver只适合短时间的计算任务,因为它的生命周期非常短

初学Android,BroadcastReceiver之发送接收广播的更多相关文章

  1. Android -- 怎么发出和接收广播, Broadcast, 电话拨号拦截,短信拦截

    1. 发送广播 使用以下三个API可以发送广播 public void click(View view){ Intent intent = new Intent(); intent.setAction ...

  2. android中如何发送一个广播

    1.首先要声明广播 private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public  ...

  3. android 发送自定义广播以及接收自定义广播

    发送自定义广播程序: 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...

  4. Android BroadcastReceiver 接收收到短信的广播

    一.知识介绍 1.broadcastReceiver是广播接受者,四大组件之一. 2.Android中内置了很多系统级别的广播,可以在应用程序中得到各种系统的状态信息. 3.使用场景: ①当手机没有电 ...

  5. Android学习之发送及接收广播

    1.使用标准广播 1.1 定义广播接收器 public class MyBroadcastReceiver extends BroadcastReceiver { @Override public v ...

  6. BroadcastReceiver之发送自定义无序广播

    首先,发送一个自定义广播:(用sendBroadcast(intent)发送一条无序广播) public void click(View v){ Intent intent = new Intent( ...

  7. 无废话Android之activity的生命周期、activity的启动模式、activity横竖屏切换的生命周期、开启新的activity获取他的返回值、利用广播实现ip拨号、短信接收广播、短信监听器(6)

    1.activity的生命周期 这七个方法定义了Activity的完整生命周期.实现这些方法可以帮助我们监视其中的三个嵌套生命周期循环: (1)Activity的完整生命周期 自第一次调用onCrea ...

  8. Android应用程序发送广播(sendBroadcast)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6744448 前面我们分析了Android应用程 ...

  9. Android 发送自定义广播(标准和本地)

    1.首先自定义一个广播接收器:MyBroadcastReceiver package example.com.mybroadcastreceiver; import android.content.B ...

随机推荐

  1. Salt Master报错:Minion did not return. [No response]

    在salt master端执行salt ‘*’ test.ping时,某一节点出现如下报错:Minion did not return. [No response] 登陆到这一节点查看minion的日 ...

  2. (2)JSTL的fmt国际化标签库

    format标签库:做国际化格式化,分两类 : 国际化核心标签:<fmt:setLocale>.<fmt:bundle>.<fmt:setBundle>.<f ...

  3. 【求助】NdisSend,自定义数据包发送失败?

    做ndis hook的时候,自定义了一个数据包,包结构应该没有问题,填充NDIS_PACKET结构是这样的,先初始化:        NdisAllocatePacketPool(&nStat ...

  4. 【树形背包】bzoj4033: [HAOI2015]树上染色

    仔细思考后会发现和51nod1677 treecnt有异曲同工之妙 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并 ...

  5. MYSQL数据库SQL语句集锦

    *特别说明:FILED代表数据表字段,CONDITIONS代表where之后的条件,TABLENAME代表数据表名   []中括号内的内容代表 可有可无. 创建数据库 create  database ...

  6. 蓝牙学习(4) -- L2CAP

    L2CAP in protocol 首先看一下L2CAP在Bluetooth protocol architecture diagram中的位置: Features of L2CAP Logical ...

  7. perl学习之六:变量作用域

    变量作用域 (一)包变量 $x = 1 这里,$x是个包变量.关于包变量,有2件重要的事情要了解: 1)假如没有其他申明,变量就是包变量:2)包变量总是全局的. 全局意味着包变量在每个程序里总可访问到 ...

  8. LeetCode(150) Evaluate Reverse Polish Notation

    题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, ...

  9. SAXException Parser configuration problem: namespace reporting is not enabled

    问题描述: 用sax的方式组装xml,在tomcat上正常运行,在weblogic12c上报错: SAXException Parser configuration problem: namespac ...

  10. python基础-文件和目录

    字符串小练习 >>> s="1a2a3a4a5a" >>> s1=s.split('a') >>> >>> ...