android组件之间的通信有多种实现方式,Broadcast就是其中一种。在activity和fragment之间的通信,broadcast用的更多本文以一个activity为例。

效果如图:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_marginLeft="27dp"
android:layout_marginTop="26dp"
android:text="发送广播" /> </LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

    private Button btn;
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.textView1); //接收广播
LocalBroadcastManager broadcastManager = LocalBroadcastManager
.getInstance(MainActivity.this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.test1");
BroadcastReceiver mItemViewListClickReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
tv.setText("1111");
}
};
broadcastManager.registerReceiver(mItemViewListClickReceiver,
intentFilter); btn = (Button) this.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { //发送广播
Intent intent = new Intent("com.example.test1");
LocalBroadcastManager.getInstance(MainActivity.this)
.sendBroadcast(intent);
}
});
}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏的更多相关文章

  1. Android中的数据存储(二):文件存储 2017-05-25 08:16 35人阅读 评论(0) 收藏

    文件存储 这是本人(菜鸟)学习android数据存储时接触的有关文件存储的知识以及本人自己写的简单地demo,为初学者学习和使用文件存储提供一些帮助.. 如果有需要查看SharedPreference ...

  2. jqueryEasyUI:tabs扩展:给tabs组件绑定双击事件 分类: JqueryEasyUI 2014-09-29 14:36 537人阅读 评论(0) 收藏

    实现代码: $.extend($.fn.tabs.methods, { /** * 绑定双击事件 * @param {Object} jq * @param {Object} caller 绑定的事件 ...

  3. android开发之broadcast学习笔记 分类: android 学习笔记 2015-07-19 16:33 32人阅读 评论(0) 收藏

    android中的广播用的太多了,今天稍微总结一下. 按注册方式分为两种: 1.静态注册广播: 静态注册广播就是在androidManifest.xml文件中注册广播,假设我们要实现这样一个效果,在一 ...

  4. 基于命令行编译打包phonegap for android应用 分类: Android Phonegap 2015-05-10 10:33 73人阅读 评论(0) 收藏

    也许你习惯了使用Eclipse编译和打包Android应用.不过,对于使用html5+js开发的phonegap应用,本文建议你抛弃Eclipse,改为使用命令行模式,绝对的快速和方便. 一直以来,E ...

  5. Android 应用中十大常见 UX 错误 分类: H1_ANDROID 2013-09-21 13:59 404人阅读 评论(0) 收藏

    转载自:http://www.apkbus.com/android-5661-1.html 摘要: Android 开发者关系团队每天都会试用无数的 App 或者受到无数的开发者发来的请求评测的 Ap ...

  6. Android中的消息机制:Handler消息传递机制 分类: H1_ANDROID 2013-10-27 22:54 1755人阅读 评论(0) 收藏

    参考<疯狂android讲义>第2版3.5 P214 一.背景 出于性能优化考虑,Android的UI操作并不是线程安全的,这意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为 ...

  7. 给EditText的drawableRight属性的图片设置点击事件 分类: 学习笔记 android 2015-07-06 13:20 134人阅读 评论(0) 收藏

    这个方法是通用的,不仅仅适用于EditText,也适用于TextView.AutoCompleteTextView等控件. Google官方API并没有给出一个直接的方法用来设置右边图片的点击事件,所 ...

  8. SQL Server阻止了对组件xp_cmdshell过程的解决方案 分类: SQL Server 2015-03-05 08:31 305人阅读 评论(0) 收藏

    SQL Server阻止了对组件xp_cmdshell过程的解决方案 错误描述:SQL Server阻止了对组件'xp_cmdshell'的过程'sys.xp_cmdshell'的访问.因为此组件已作 ...

  9. android开发之调试技巧 分类: android 学习笔记 2015-07-18 21:30 140人阅读 评论(0) 收藏

    我们都知道,android的调试打了断点之后运行时要使用debug as->android application 但是这样的运行效率非常低,那么我们有没有快速的方法呢? 当然有. 我们打完断点 ...

随机推荐

  1. [原博客] POI系列(1)

    正规.严谨.精妙. -POI 发现POI(波兰信息学奥赛)的题都很有意思.于是开刷bzoj上的poi题目(按ac人数降序..).顺手写一写题解,加深印象. 为了防止一篇文章过于长,打算每五道题另起一篇 ...

  2. 【HDU 3949】 XOR (线性基,高斯消元)

    XOR Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  3. OA学习笔记-005-Spring2.5与struts2.1整合

    一.单独测试strust 1.action package cn.itcast.oa.test; import org.springframework.context.annotation.Scope ...

  4. [OJ] Find Minimum in Rotated Sorted Array

    LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...

  5. [cocos2dx动作]CCLabel类数字变化动作

    cococs2dx的CCLabel类的数字变化动作 介绍: 简单的数字变化动作(适用于CCLabel类对象, 包括CCLabelTTF, CCLabelAtlas, CCLabelBMFont等等) ...

  6. Nginx 安装成 Windows 服务

    Nginx 安装成Windows 服务方法,具体方法如下 1. 下载nginx windows版本 http://www.nginx.org 2. 下载微软的2个工具: instsrv.exe.srv ...

  7. 基础算法(搜索):NOIP 2015 斗地主

    Description 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3& ...

  8. 关于ArrayList线程安全解决方案

    一:使用synchronized关键字 二:使用Collections.synchronizedList();使用方法如下: 假如你创建的代码如下:List<Map<String,Obje ...

  9. Linux Mono Asp.net 部署方案

    1.Jexus 国内的 官网:http://www.jexus.org 2.Apache 官网:http://mono-project.com/Mod_mono 3.Nginx 官网:http://m ...

  10. JDBC_demo:java连接mysql过程

    1.任何数据库驱动程序都提供对java.sql.Driver接口的驱动类,mysql-connector-java-5.1.39-bin.jar中Driver: package com.mysql.j ...