当前位置:网站首页>Cross process two-way communication using messenger
Cross process two-way communication using messenger
2022-06-13 06:22:00 【Novice Xiaowang】
Messenger : messenger
1. Client side usage Activity
private Messenger messenger;// messenger
// Handle messages from the server
Handler handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MessagerService.MSG:
Log.e("TAG"," Received a response from the server :"+msg.getData().getString("msg"));
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Binding server
Intent intent = new Intent(this,MessagerService.class);
bindService(intent,connection,BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// establish Messenger from client to server
messenger = new Messenger(service);
Message message = Message.obtain(null,MessagerService.MSG);
Bundle bundle = new Bundle();
bundle.putString("msg"," Client message : Hello Server side ");
message.setData(bundle);
// take messenger Pass it to the server Create a server to client messenger
message.replyTo = new Messenger(handler);
try {
// Send message to server
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};2. Server use service
public static final int MSG = 1;
public Handler handler = new Handler(){
@SuppressLint("HandlerLeak")
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG:
Log.e("TAG",msg.getData().getString("msg"));
Log.e("TAG"," Received client message ");
// Get the client's Messenger object
Messenger messenger = msg.replyTo;
Message message = Message.obtain(null,MessagerService.MSG);
Bundle bundle = new Bundle();
bundle.putString("msg"," Hello, client , I've got the message ");
message.setData(bundle);
try {
messenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
}
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new Messenger(handler).getBinder();
}3.manifest in
<service android:name=".MessagerService" android:process=":remote"/>bring activity and service Not in a thread
边栏推荐
- Using the shutter floor database framework
- Applet pull-up loading data
- Dynamic link library nesting example
- Regular verification of mobile phone number, landline email ID card
- 【案例】一个超级好用的工具 —— 给程序员用的计算器
- AI实现亲人“复活”|老照片修复|老照片上色,免费APP推荐
- JVM基础
- Local file search tool everything
- [2022 college entrance examination season] what I want to say as a passer-by
- Dart class inherits and implements mixed operators
猜你喜欢
不在以下合法域名列表中,微信小程序解决办法

自定义View —— 可伸展的CollapsExpendView

Echart line chart: different colors are displayed when the names of multiple line charts are the same

Echart柱状图:x轴显示value,y轴显示类别

《MATLAB 神经网络43个案例分析》:第11章 连续Hopfield神经网络的优化——旅行商问题优化计算

Explication détaillée du triangle Yang hui

Dart class inherits and implements mixed operators

Echart histogram: stacked histogram displays value

AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app

php 分布式事务 原理详解
随机推荐
Echart histogram: echart implements stacked histogram
c语言对文件相关的处理和应用
高迸发解决方案2
SSM框架整合--->简单后台管理
JS convert text to language for playback
A brief analysis of the overall process of view drawing
自定义View —— 可伸展的CollapsExpendView
微信小程序:基础复习
1+1 > 2, share creators can help you achieve
App performance test: (II) CPU
Uni app dynamic setting title
[var const let differences]
Introduction to USB learning (I) -- Dongfeng night flower tree
杨辉三角形详解
[JS] array de duplication
Usegeneratedkeys=true configuration
Detailed explanation of PHP distributed transaction principle
JVM基础
The Boys x PUBGMOBILE 联动火热来袭!来看最新游戏海报
端午安康,使用祝福话语生成词云吧