【Android】使用 Broadcast 实现进程间通讯
2023-09-11 14:20:52 时间
1 Broastcast 简介
Broadcast(广播)是 Android 中一种广泛运用的在应用程序之间传输信息的机制。使用 Broadcast 能够很方便得实现进程间通讯,一端通过 sendBroadcast(intent) 广播消息,另一端通过 registerReceiver(myReceiver) 注册 BroadcastReceiver(接收器)接收消息。
2 clien_A 代码
(1)主 Activity
MainActivity.java
package com.zhyan8.client_A;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et_msg;
private Button btn_send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
et_msg = (EditText) findViewById(R.id.et_msg);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(cl);
}
View.OnClickListener cl = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId()==R.id.btn_send) {
String str = et_msg.getText().toString();
Intent intent = new Intent();
intent.setAction("com.zhyan8.client_A.MainActivity");
Bundle bundle = new Bundle();
bundle.putString("msg_A", "from client_A, msg: " + str);
intent.putExtras(bundle);
sendBroadcast(intent);
}
}
};
}
(2)布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.zhyan8.client_A.MainActivity">
<EditText
android:id="@+id/et_msg"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text=""
android:background="#ffcc66"/>
<Button
android:id="@+id/btn_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:textSize="40sp"
android:text="发送"/>
</LinearLayout>
界面如下:
3 client_B 代码
(1)主 Activity
MainActivity.java
package com.zhyan8.client_B;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv_msg;
private IntentFilter intentFilter;
private MyReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
tv_msg = (TextView) findViewById(R.id.tv_msg);
intentFilter = new IntentFilter();
intentFilter.addAction("com.zhyan8.client_A.MainActivity");
myReceiver = new MyReceiver();
registerReceiver(myReceiver, intentFilter);
}
private class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
String str = bundle.getString("msg_A");
tv_msg.setText(str);
}
}
}
(2)布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.zhyan8.client_B.MainActivity">
<TextView
android:id="@+id/tv_msg"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffcc66"/>
</LinearLayout>
界面如下:
4 效果展示
在 client_A 端编辑文本【Qwert】,如下:
点击【发送】按钮后,在client_B端收到消息【from client_A, msg: Qwert】,如下:
相关文章
- Android-进程和线程
- [Android Pro] Android 进程级别 和 oom_adj对应关系
- [Android Traffic] 让android应用在传输网络数据的时候更省电
- JAVA Eclipse如何修改Android程序名称
- 修改android最小堆内存
- 95、Android下在onCreate中获取控件的宽度和高度(通过回调)
- Android View的事件分发
- macos安装android studio(Android Studio 2021.1.1)
- Android应用程序与SurfaceFlinger服务之间的共享UI元数据(SharedClient)的创建过程分析 .
- 【VLC-Android】vlc-android简例
- Android stuido 解决Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file
- Android 多进程使用场景
- Android 编译之android.bp详解
- Android 系统服务 守护进程服务分析
- Android 11.0 12.0进程保活白名单功能实现
- Android 10.0 设置默认息屏时间
- 【错误记录】Android Studio 集成 ARoute 编译报错 ( 兼容 support 库和 androidx 库 | add ‘tools:replace=“android:appCo )
- 【Android 进程保活】应用进程拉活 ( 双进程守护 + JobScheduler 保活 | 成功率最高 | 推荐使用 )
- 【Android 进程保活】应用进程拉活 ( 系统 Service 机制拉活 | Service 组件 onStartCommand 方法分析 | 源码资源 )
- Android - TextView Ellipsize属性
- Android_通过传感器抓小偷
- Android实战技巧之十九:android studio导出jar包(Module)并获得手机信息
- Android 儿子Activity在启动过程中的流程组件 && 儿子Activity在一个新的进程组件启动过程
- Android Studio 4.2.2 Kotlin的import kotlinx.android.synthetic.main.(layout的xml名).view.*
- Android的java的报错提示:Could not find com.android.tools.build:gradle:4.2.2.
- Android Studio NDK报错:mips64el-linux-android-strip 找不到