使用Broadcast實(shí)現(xiàn)Android組件間的通信
Android組件之間的通信有多種實(shí)現(xiàn)方式,Broadcast就是其中一種。在activity和fragment之間的通信,broadcast用的更多本文以一個(gè)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="發(fā)送廣播" /> </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) { //發(fā)送廣播 Intent intent = new Intent("com.example.test1"); LocalBroadcastManager.getInstance(MainActivity.this) .sendBroadcast(intent); } }); } }
原文鏈接:http://blog.csdn.net/u012702547/article/details/46816331
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android之網(wǎng)絡(luò)通信案例分析
- Android Handler主線程和一般線程通信的應(yīng)用分析
- 深入理解Android組件間通信機(jī)制對(duì)面向?qū)ο筇匦缘挠绊懺斀?/a>
- Android組件間通信--深入理解Intent與IntentFilter
- Android 進(jìn)程間通信實(shí)現(xiàn)原理分析
- android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式實(shí)例分享
- Android中Socket通信的實(shí)現(xiàn)方法概述
- Android提高之Android手機(jī)與BLE終端通信
- python服務(wù)器與android客戶端socket通信實(shí)例
- 淺析Android系統(tǒng)中HTTPS通信的實(shí)現(xiàn)
相關(guān)文章
android中寫一個(gè)內(nèi)部類來選擇文件夾中指定的圖片類型實(shí)例說明
選擇文件夾中指定的圖片類型,本類是用來選擇文件夾中是.jpg類型的圖片具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06Android使用SQLite數(shù)據(jù)庫的示例
本篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01Android Support Annotations資料整理
這篇文章主要介紹了Android Support Annotations資料整理的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 中Fragment與Activity通訊的詳解
這篇文章主要介紹了Android 中Fragment與Activity通訊的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握如何通信的,需要的朋友可以參考下2017-10-10Android動(dòng)畫之漸變動(dòng)畫(Tween Animation)詳解 (漸變、縮放、位移、旋轉(zhuǎn))
這篇文章主要介紹了Android動(dòng)畫之漸變動(dòng)畫(Tween Animation)用法,結(jié)合實(shí)例形式詳細(xì)分析了Android漸變動(dòng)畫Tween Animation實(shí)現(xiàn)漸變,縮放,位移,旋轉(zhuǎn)等技巧,需要的朋友可以參考下2016-01-01Android實(shí)戰(zhàn)之Cocos游戲容器搭建
這篇文章主要介紹了Android實(shí)戰(zhàn)之Cocos游戲容器搭建,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-06-06android當(dāng)前apn的狀態(tài)以及獲取方法
在絕大多數(shù)android機(jī)器etc路徑下存放一個(gè)的apns-conf.xml文件,表示當(dāng)前機(jī)器使用的apn信息通過root機(jī)器可以push出來看看,具體路徑可以上網(wǎng)搜下,接下來介紹獲取apn的狀態(tài)的方法2013-01-01Android BottomSheet效果的兩種實(shí)現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實(shí)現(xiàn)方式,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08一個(gè)簡(jiǎn)單的Android軌跡動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的Android軌跡動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09