当前位置:网站首页>AIDL communication between two APPs
AIDL communication between two APPs
2022-08-04 06:05:00 【N_Y_S】
创建服务端
一、创建服务端App
项目名:power
包名:com.nys.power
aidl:是用于定义服务器和客户端通信接口的一种描述语言,Mainly used for communication between processes;
二、创建AIDL文件,Only the business logic interface is made,Rebuild项目,Let the project generate an interface file with the same name
The corresponding package name is automatically created
在aidlAdd a logical interface to the file
interface PowerAidlInterface {
void setAccount(String user,String pwd);
String getInfo();
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
After adding the logical interfaceRebuild Let the project generate an interface file with the same name
三、创建一个Java类,Inherit the abstract class in the interface file(Stub),并完成业务逻辑
public class PowerImpl extends PowerAidlInterface.Stub{
private String user;
private String pwd;
@Override
public void setAccount(String user, String pwd) throws RemoteException {
this.user=user;
this.pwd=pwd;
}
@Override
public String getInfo() throws RemoteException {
String[] userarray=new String[]{"admin","user"};
String[] pwdarray=new String[]{"123","111"};
for (int i=0;i<userarray.length;i++){
if (user.equals(userarray[i])){
if (pwd.equals(pwdarray[i])){
return "登录成功";
}
}
}
return "登陆失败";
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
}
四、expose the interface,使用Service
public class PowerService extends Service {
public PowerService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new PowerImpl();
}
}
创建客户端
一、创建客户端App
项目名称:login
包名:com.nys.login
二、将服务器的,aidl文件复制到客户端app中,Rebuid,Let the project generate an interface file with the same name
Display the file in the explorer,Then copy the file to the client'ssrc文件夹下,然后Rebuid
三 、在Activity中绑定接口,使用接收到的Binder通信
public class MainActivity extends AppCompatActivity {
TextInputEditText text_user;
TextInputEditText text_pwd;
Button btn_login;
PowerAidlInterface powerAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_user=findViewById(R.id.text_user);
text_pwd=findViewById(R.id.text_pwd);
btn_login=findViewById(R.id.btn_login);
Intent intent=new Intent();
intent.setComponent(new ComponentName("com.nys.power","com.nys.power.PowerService"));
bindService(intent,conn,BIND_AUTO_CREATE);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = text_user.getText().toString();
String pwd = text_pwd.getText().toString();
try {
powerAidlInterface.setAccount(user,pwd);
String result = powerAidlInterface.getInfo();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
powerAidlInterface=PowerAidlInterface.Stub.asInterface(iBinder);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
}
At this time, the communication is not successful,因为版本原因,The path to the server needs to be added to the registry
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.nys.login">
<queries >
<package android:name="com.nys.power"/>
</queries>
...
</manifest>
边栏推荐
猜你喜欢
(十二)树--哈夫曼树
攻防世界MISC—MISCall
Kubernetes基本入门-元数据资源(四)
Zend FrameWork RCE1
flink sql left join数据倾斜问题解决
NFT市场以及如何打造一个NFT市场
(五)栈及其应用
MySQL事务详解(事务隔离级别、实现、MVCC、幻读问题)
Logistic Regression --- Introduction, API Introduction, Case: Cancer Classification Prediction, Classification Evaluation, and ROC Curve and AUC Metrics
网络大作业心得笔记
随机推荐
iptables防火墙
TensorFlow2学习笔记:4、第一个神经网模型,鸢尾花分类
浏览器中的同源策略
数据库根据提纲复习
flink-sql所有语法详解
postgres 递归查询
flink onTimer定时器实现定时需求
关系型数据库-MySQL:约束管理、索引管理、键管理语句
智能合约安全——delegatecall (2)
NFT市场以及如何打造一个NFT市场
Vulnhub:Sar-1
将两个DataTable合并——DataTable.Merge 方法
剑指 Offer 2022/7/5
Kubernetes集群安装
flink-sql所有数据类型
剑指 Offer 2022/7/9
PostgreSQL模式(Schema)
flink问题整理
网络大作业心得笔记
多项式回归(PolynomialFeatures)