使用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?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例
這篇文章主要為大家介紹了Android?懸浮按鈕之實(shí)現(xiàn)兔兔按鈕示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Android實(shí)現(xiàn)界面內(nèi)嵌多種卡片視圖(ViewPager、RadioGroup)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)界面內(nèi)嵌多種卡片視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android RecyclerView添加FootView和HeadView
這篇文章主要介紹了Android RecyclerView添加FootView和HeadView的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Android實(shí)現(xiàn)View滑動(dòng)的6種方式
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)View滑動(dòng)的6種方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解
這篇文章主要介紹了Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08
Eclipse NDK遷移到Android Studio的方法示例
本篇文章主要介紹了Eclipse NDK遷移到Android Studio的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android 實(shí)現(xiàn)永久性開啟adb 的root權(quán)限
這篇文章主要介紹了Android 實(shí)現(xiàn)永久性開啟adb 的root權(quán)限,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析
這篇文章主要為大家介紹了OKhttp攔截器實(shí)現(xiàn)實(shí)踐環(huán)節(jié)源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

