使用Broadcast实现android组件之间的通信 分类: android 学习笔记 2015-07-09 14:16 110人阅读 评论(0) 收藏
2023-09-27 14:27:41 时间
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);
}
});
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章
- android 自定义组件-带图片的textView
- Android 线程 thread 两种实现方法
- android基础组件---->Picker的使用
- Android View焦点总结
- Android Jetpack架构组件(十)之Slices
- Android Jetpack架构组件(二)之Lifecycle
- 1-3年Android开发工程师面试经验分享,安卓系列学习进阶视频
- 十、Android学习第九天——小结(转)
- [Android学习笔记4]四大应用组件之一:Service 上
- android 组件内部实现触摸事件,更改背景
- Android事件处理下(按键、触摸屏和滚动球的一些实现细节)
- Mosquitto搭建Android推送服务(二)Mosquitto简介及搭建
- android四大组件
- Android build-tools升级到23.0.0_rc1无法解决编译后的问题
- Gradle 载入中 Android 下一个.so档
- 【Android】事件总线(解耦组件) EventBus 详解
- android 调用系统的音乐和视频播放器
- Android彻底组件化demo
- Android JetPack底部导航Navigation 组件的介绍与使用
- Android JetPack组件之Room数据库的集成与详解
- 【3】android 四大组件详解
- Android开发 解决AlertDialog中的EditText无法调出输入法的问题
- android 快速开发之复制当前行
- Android 使用按键精灵获取当前运行的app的组件名称
- 【Android 组件化】路由组件 ( 注解处理器调试 )
- Android如何查找应用中调用的系统资源
- Android 4.0设置Dialog点击屏幕不消失
- Android 软引用(SoftReference)与LruCache
- Android下拉刷新、滚动到底部自动加载更多RecyclerView组件
- Android自定义控件——开源组件SlidingMenu的项目集成
- RN系列:RN使用Android原生控件或自定义组件
- 【Android 进程保活】应用进程拉活 ( 系统 Service 机制拉活 | Service 组件 onStartCommand 方法分析 | 源码资源 )
- 【Android 内存优化】内存抖动 ( 垃圾回收算法总结 | 分代收集算法补充 | 内存抖动排查 | 内存抖动操作 | 集合选择 )
- 我的Android进阶之旅------>解决Android Studio编译后安装apk报错:The APK file does not exist on disk
- Android系统移植与调试之------->如何修改Android设备添加重启、飞行模式、静音模式等功能(一)