安卓IPC跨进程通讯:AIDL+Retrofit——AndLin
使用场景
需要用到安卓跨进程通讯,IPC (进程间通信) 的时候,AndLinker是一款Android上的IPC (进程间通信) 库,结合了AIDL和Retrofit的诸多特性,且可以与RxJava和RxJava2的Call Adapters无缝结合使用。
个人简单理解就是:简化AIDL流程的一个第三方库。使用时需要先了解一下AIDL、retrofit。
功能特性
以普通Java接口代替AIDL接口
像Retrofit一样生成远程服务接口的IPC实现
支持的Call Adapters:Call,RxJava Observable,RxJava2 Observable & Flowable
支持远程服务回调机制
支持AIDL的所有数据类型
支持AIDL的所有数据定向tag:in,out,inout
支持AIDL的oneway关键字
配置
在服务端以及客户端的项目根目录的build.gradle中添加jcenter()仓库
allprojects {
repositories {
jcenter()
}
}
在App的build.gradle中添加如下依赖
dependencies { implementation'com.codezjx.library:andlinker:0.7.1'}
支持的数据类型
AndLinker支持AIDL所有数据类型:
Java语言中的所有原始类型 (如:int,long,char,boolean,等等)
String
CharSequence
Parcelable
List (List中的所有元素必须是此列表中支持的数据类型)
Map (Map中的所有元素必须是此列表中支持的数据类型)
开始
1.服务端中创建一个interface,并且使用@RemoteInterface标注。
接口里的方法就是按需求需创建。这里只举几个简单的示例。
package skyf.mydemo.aidl;
import com.codezjx.andlinker.annotation.RemoteInterface;
/**
* Created by Skyf on 2018/5/3.
*/
@RemoteInterface
public interface IRemoteService {
int getPid();
String getUserName();
void basicTypes(int anInt,long aLong,boolean aBoolean,float aFloat,
double aDouble, String aString);
}
2.服务端创建一个Service,并在清单文件中声明action,等待客户端调用。
package skyf.mydemo.aidl;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import com.codezjx.andlinker.AndLinkerBinder;
/**
* Created by Skyf on 2018/5/3.
*/
public class RemoteServiceextends Service {
private AndLinkerBindermAndLinkerBinder;
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.i("ANDLINKER_AIDL_SERVICE","RemoteService.onBind");
return mAndLinkerBinder;
}
@Override
public void onCreate() {
super.onCreate();
Log.i("ANDLINKER_AIDL_SERVICE","RemoteService.onCreate");
mAndLinkerBinder = AndLinkerBinder.Factory.newBinder();
mAndLinkerBinder.registerObject(mIRemoteService);
}
IRemoteServicemIRemoteService =new IRemoteService() {
@Override
public int getPid() {
return 1111;
}
@Override
public String getUserName() {
return "刘备";
}
@Override
public void basicTypes(int anInt,long aLong,boolean aBoolean,float aFloat,double aDouble, String aString) {
Log.i("ANDLINKER_AIDL_SERVICE","anInt:" + anInt);
Log.i("ANDLINKER_AIDL_SERVICE","aLong:" + aLong);
Log.i("ANDLINKER_AIDL_SERVICE","aBoolean:" + aBoolean);
Log.i("ANDLINKER_AIDL_SERVICE","aFloat:" + aFloat);
Log.i("ANDLINKER_AIDL_SERVICE","aDouble:" + aDouble);
Log.i("ANDLINKER_AIDL_SERVICE","aString:" + aString);
}
};
}
3.复制IRemoteService到客户端中
4.进行连接、通讯
package com.skyf.myaidltest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.codezjx.andlinker.AndLinker;
public class MainActivityextends AppCompatActivity {
private TextViewtvContent;
private TextViewtvContent1;
private TextViewtvContent2;
private TextViewtvContent3;
private TextViewtvContent4;
private TextViewtvContent5;
private TextViewtvContent6;
private AndLinkermLinker;
private IRemoteServicemRemoteService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initAidl();
tvContent = findViewById(R.id.tv_content);
tvContent1 = findViewById(R.id.tv_content1);
tvContent2 = findViewById(R.id.tv_content2);
tvContent3 = findViewById(R.id.tv_content3);
tvContent4 = findViewById(R.id.tv_content4);
tvContent5 = findViewById(R.id.tv_content5);
tvContent6 = findViewById(R.id.tv_content6);
findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int pid =mRemoteService.getPid();
String userName =mRemoteService.getUserName();
Log.i("ANDLINKER_AIDL_CLICENT","getPid:" + pid);
Log.i("ANDLINKER_AIDL_CLICENT","getUserName" + userName);
tvContent.setText(String.valueOf(pid));
tvContent1.setText(userName);
}
});
findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRemoteService.basicTypes(1,222,true,0.01f,20.20d,"test");
}
});
}
private void initAidl() {
mLinker =new AndLinker.Builder(this)
.packageName("skyf.mydemo")//服务端app包名
.action("skyf.mydemo.MYADIL")//服务端服务的action
.build();
mLinker.bind();
mRemoteService =mLinker.create(IRemoteService.class);
}
}