aidl是什么?
进程之前共享数据的一种方式
android studio上aidl的使用
如下为src的项目结构
1.在main下创建aidl文件夹,然后新建包,新建一个aidl文件 如上图中标记1
代码如下
// AidlService.aidl
package ktc.com.aidlservice;
interface AidlService {
//我随便定义一个方法
String getstring();
}
2.build项目,使AidlService.aidl生成AidlService如上图标记2
代码如下
其为自动生成
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: F:\\project\\aidlservice\\app\\src\\main\\aidl\\ktc\\com\\aidlservice\\AidlService.aidl
*/
package ktc.com.aidlservice;
// Declare any non-default types here with import statements
public interface AidlService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements ktc.com.aidlservice.AidlService
{
private static final java.lang.String DESCRIPTOR = "ktc.com.aidlservice.AidlService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an ktc.com.aidlservice.AidlService interface,
* generating a proxy if needed.
*/
public static ktc.com.aidlservice.AidlService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof ktc.com.aidlservice.AidlService))) {
return ((ktc.com.aidlservice.AidlService)iin);
}
return new ktc.com.aidlservice.AidlService.Stub.Proxy(obj);
}
@Override public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getstring:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getstring();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements ktc.com.aidlservice.AidlService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
@Override public java.lang.String getstring() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getstring, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_getstring = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String getstring() throws android.os.RemoteException;
}
3在java目录下进行编写调用Service代码
代码如下
package ktc.com.aidlservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by daic on 2018/9/7.
*/
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return new Mybinder();
}
@Override
public void onCreate() {
Log.w("dz", "serviceonCreate: " );
super.onCreate();
}
private class Mybinder extends AidlService.Stub {
@Override
public String getstring() throws RemoteException {
Log.w("dz", "getstring: ");
return "我是aidlservice中的代码";
}
}
}
记得注册
<service android:name=".MyService"
android:process=":remote"
android:exported="true">
<intent-filter>
<action android:name="com.ktc.aidls"></action>
</intent-filter>
</service>
此时,服务端已经构建完毕
接下来编写客户端
如果在同一个app进行进程之间的通讯,则直接贴代码如下
package ktc.com.aidlservice;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private AidlService iService;
private TextView tv;
private static final String BOUNDSERVICE_PACKAGE = "ktc.com.aidlservice";
private Button bt;
public class conn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iService = AidlService.Stub.asInterface(iBinder);
Log.w("dz", "onServiceConnected: ");
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intentService = new Intent();
intentService.setAction("com.ktc.aidls");
intentService.setPackage(BOUNDSERVICE_PACKAGE);
conn myconnects=new conn();
boolean b = bindService(intentService, myconnects, BIND_AUTO_CREATE);
Log.w("dz", "onCreate: "+b+"" );
tv = findViewById(R.id.tvs);
bt = findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.w("dz", "onClick: " );
try {
tv.setText(iService.getstring());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
}
到这块已经aidl已经完了
但是如果是两个app之间进行通讯,那么就有点不同了。
因为当你把服务端搭好,你把上面客户端代码贴上去 你会发现报错,所以需要把最开始搭建服务器时候的图片中生成的文件2复制到另一个app中。但是要注意这时候这个文件需要放在src下面,并且包名需要和服务端的报名一致。
到这里最简单的一个aidl就写完了