Android IPCMessenger实现跨进程双向通信
Posted 王睿丶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android IPCMessenger实现跨进程双向通信相关的知识,希望对你有一定的参考价值。
效果图:
MyService.java
public class MyService extends Service {
private Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
//客户端→服务端
Bundle bundle = msg.getData();
bundle.setClassLoader(MyBean.class.getClassLoader());
MyBean myBean = bundle.getParcelable("message");
Toast.makeText(MyService.this,myBean.getName(),Toast.LENGTH_SHORT).show();
//服务端→客户端
try {
Messenger clientMessenger = msg.replyTo;
myBean = new MyBean();
myBean.setName("皮卡丘对王睿使用了十万伏特");
bundle = new Bundle();
bundle.putParcelable("message",myBean);
Message message = new Message();
message.setData(bundle);
message.replyTo = clientMessenger;
clientMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
private Messenger messenger = new Messenger(handler);
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return messenger.getBinder();
}
}
MyBean.java
public class MyBean implements Parcelable {
private String name;
public MyBean(){
}
protected MyBean(Parcel in) {
name = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<MyBean> CREATOR = new Creator<MyBean>() {
@Override
public MyBean createFromParcel(Parcel in) {
return new MyBean(in);
}
@Override
public MyBean[] newArray(int size) {
return new MyBean[size];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button btnIPC;
private Button btnSend;
private Messenger messengerProxy;
private Handler handler = new Handler(Looper.getMainLooper()){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
bundle.setClassLoader(MyBean.class.getClassLoader());
MyBean myBean = bundle.getParcelable("message");
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,myBean.getName(),Toast.LENGTH_SHORT).show();
}
},3000);
}
};
private Messenger clientMessenger = new Messenger(handler);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnIPC = findViewById(R.id.btn_ipc);
btnIPC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,MyService.class);
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder iBinder) {
messengerProxy = new Messenger(iBinder);
Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, Context.BIND_AUTO_CREATE);
}
});
btnSend = findViewById(R.id.btn_send);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyBean myBean = new MyBean();
myBean.setName("王睿对皮卡丘使用了精灵球");
try {
Message message = new Message();
message.replyTo = clientMessenger;
Bundle bundle = new Bundle();
bundle.putParcelable("message",myBean);
message.setData(bundle);
messengerProxy.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="50dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_ipc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="IPC连接"/>
<Button
android:id="@+id/btn_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="IPC通信"/>
</LinearLayout>
AndroidManifest.xml
增加
<service
android:name=".MyService"
android:process=":wangrui"></service>
Demo地址:
链接:https://pan.baidu.com/s/18myZEzoVNBgP5LTB8L2iVA
提取码:mc3i
以上是关于Android IPCMessenger实现跨进程双向通信的主要内容,如果未能解决你的问题,请参考以下文章