使用Broadcast實現(xiàn)Android組件間的通信
Android組件之間的通信有多種實現(xiàn)方式,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="發(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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android之網(wǎng)絡(luò)通信案例分析
- Android Handler主線程和一般線程通信的應(yīng)用分析
- 深入理解Android組件間通信機制對面向?qū)ο筇匦缘挠绊懺斀?/a>
- Android組件間通信--深入理解Intent與IntentFilter
- Android 進程間通信實現(xiàn)原理分析
- android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式實例分享
- Android中Socket通信的實現(xiàn)方法概述
- Android提高之Android手機與BLE終端通信
- python服務(wù)器與android客戶端socket通信實例
- 淺析Android系統(tǒng)中HTTPS通信的實現(xiàn)
相關(guān)文章
android中寫一個內(nèi)部類來選擇文件夾中指定的圖片類型實例說明
選擇文件夾中指定的圖片類型,本類是用來選擇文件夾中是.jpg類型的圖片具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06Android使用SQLite數(shù)據(jù)庫的示例
本篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Android Support Annotations資料整理
這篇文章主要介紹了Android Support Annotations資料整理的相關(guān)資料,需要的朋友可以參考下2017-05-05Android 中Fragment與Activity通訊的詳解
這篇文章主要介紹了Android 中Fragment與Activity通訊的詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握如何通信的,需要的朋友可以參考下2017-10-10Android動畫之漸變動畫(Tween Animation)詳解 (漸變、縮放、位移、旋轉(zhuǎn))
這篇文章主要介紹了Android動畫之漸變動畫(Tween Animation)用法,結(jié)合實例形式詳細分析了Android漸變動畫Tween Animation實現(xiàn)漸變,縮放,位移,旋轉(zhuǎn)等技巧,需要的朋友可以參考下2016-01-01Android BottomSheet效果的兩種實現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實現(xiàn)方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08