在AIDL中客户端和服务端传入参数 是可以设置流向.仅限参数不包含返回值
1. in :客户端可以传入参数到服务到(默认方法)
2. out:服务端修改客户端传入参数对象 会影响客户端的传入实例
3. inout:服务端即可接受客户端参数也可以修改对其客户端实例影响
这个标签在哪?
这里用AIDL基本使用2的Demo作为案例:AIDL基本使用2
在AIDL基本使用2案例中 IMyAidlInterface.aidl 用来作为客户端和服务端交互的接口.
// IMyAidlInterface.aidl
package com.ucoupon.myservice;
import com.ucoupon.myservice2.Book;
interface IMyAidlInterface {
String bookIn(in Book mbook);
String bookOut(out Book mbook);
String bookInout(inout Book mbook);
}
看上面的代码中可以知道.in out inout是用来修饰aidl接口中传入参数.
我们在复习下Book.java 里面有什么
package com.ucoupon.myservice2;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by FMY on 2017/5/18.
*/
public class Book implements Parcelable {
String name;
int id;
public Book(String name, int id) {
this.name = name;
this.id = id;
}
protected Book(Parcel in) {
name = in.readString();
id = in.readInt();
}
public static final Creator CREATOR = new Creator() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
}
}
一个很普通的类有两个字段
为什么我们在AIDL基本使用的2中没有报错?
继续查看源码
如果被修饰方法参数为out那么会自动创建修饰参数的空构造方法
在本例中有如下方法被out修饰:
String bookOut(out Book mbook);
解决办法:创建一个空构造方法
可以看到Book实例化调用readFromParcel方法.可是book没有这个方法.
解决办法:
在Book类中添加此方法.此方法是用于客户端用了out或者inout修饰的方法 中读取服务端修改后的对象的数值在赋值给客户端
package com.ucoupon.myservice2;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by FMY on 2017/5/18.
*/
public class Book implements Parcelable {
....
public void readFromParcel(Parcel reply) {
name = reply.readString();
id = reply.readInt();
}
.....
}
修改后编译通过;
然后把文件拷贝到客户端 进行绑定操作 这一步略过
来看服务段代码
package com.ucoupon.myservice;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import com.ucoupon.myservice2.Book;
public class MyService extends Service {
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate() called");
}
//绑定的时候回调
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind() called with: intent = [" + intent + "]");
return new MyAidlInterface();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand() called with: intent = [" + intent + "], flags = [" + flags + "], startId = [" + startId + "]");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void unbindService(ServiceConnection conn) {
Log.d(TAG, "unbindService() called with: conn = [" + conn + "]");
super.unbindService(conn);
}
@Override
public void onRebind(Intent intent) {
Log.d(TAG, "onRebind() called with: intent = [" + intent + "]");
super.onRebind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind() called with: intent = [" + intent + "]");
return super.onUnbind(intent);
}
@Override
public void unregisterReceiver(BroadcastReceiver receiver) {
Log.d(TAG, "unregisterReceiver() called with: receiver = [" + receiver + "]");
super.unregisterReceiver(receiver);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
@Override
public void onStart(Intent intent, int startId) {
Log.d(TAG, "onStart() called with: intent = [" + intent + "], startId = [" + startId + "]");
super.onStart(intent, startId);
}
private static final String TAG = "MyService";
class MyAidlInterface extends IMyAidlInterface.Stub{
@Override
public String bookIn(Book mbook) throws RemoteException {
Log.d(TAG, "bookIn() called with: mbook = [" + mbook + "]");
mbook.id = 233;
return null;
}
@Override
public String bookOut(Book mbook) throws RemoteException {
Log.d(TAG, "bookout() called with: mbook = [" + mbook + "]");
mbook.id = 233;
return null;
}
@Override
public String bookInout(Book mbook) throws RemoteException {
Log.d(TAG, "bookInout() called with: mbook = [" + mbook + "]");
mbook.id = 233;
return null;
}
}
}
客户端:
package com.ucoupon.aidlstudy;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.ucoupon.myservice.IMyAidlInterface;
import com.ucoupon.myservice2.Book;
public class MainActivity extends AppCompatActivity {
private Myconnect myconnect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//连接管理
myconnect = new Myconnect();
//意图
Intent intent = new Intent();
intent.setClassName("com.ucoupon.myservice","com.ucoupon.myservice.MyService");
startService(intent);
//开始绑定服务
bindService(intent,myconnect,BIND_AUTO_CREATE);
}
private static final String TAG = "MainActivity";
class Myconnect implements ServiceConnection {
//连接的成功的时候回调
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "onServiceConnected() called with: name = [" + name + "], service = [" + service + "]");
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
Book mBook = new Book("客户端", -1);
try {
Log.e("fmy","客户端没有调用 bookin方法前mBook: name = "+mBook.name+","+" id"+mBook.id);
iMyAidlInterface.bookIn(mBook);
Log.e("fmy","客户端调用后 bookin方法后mBook: name = "+mBook.name+","+" id"+mBook.id);
} catch (RemoteException e) {
e.printStackTrace();
}
//解绑服务
// unbindService(myconnect);
}
//断开连接的时候回调
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected() called with: name = [" + name + "]");
}
}
}
结果预测:
根据前言in 可以让客户端传入一个对象给服务端.但是服务端拿到对象后修改对客户端实例是没有效果
运行结果:
客户端:
E/fmy: 客户端没有调用 bookin方法前mBook: name = 客户端, id-1
客户端调用后 bookin方法后mBook: name = 客户端, id-1
服务端:
mbook = [Book{name='客户端', id=-1}]
从上面的日志可以发现客户端调用前后book实例是没有任何变化.
服务端也正确读取到客户端发送book实例信息.
回过头来再看看bookIn方法.
@Override
public String bookIn(Book mbook) throws RemoteException {
Log.d(TAG, "bookIn() called with: mbook = [" + mbook + "]");
mbook.id = 233;
return null;
}
方法中bookIn对传入的book对象修改了id为233.但是客户端在调用后并有没有影响自身book对象.
客户端代码和上面in测试差不多,只改变调用bookout方法而已
客户端:
Book mBook = new Book("客户端", -1);
try {
Log.e("fmy","客户端没有调用 bookout方法前mBook: name = "+mBook.name+","+" id"+mBook.id);
iMyAidlInterface.bookOut(mBook);
Log.e("fmy","客户端调用后 bookout方法后mBook: name = "+mBook.name+","+" id"+mBook.id);
} catch (RemoteException e) {
e.printStackTrace();
}
预期结果:
out定义:服务端无法读取从客户端传入的参数,但可以改变传入的对象,然后对客户端的实例对象有影响
客户端没有调用 bookout方法前mBook: name = 客户端, id-1
客户端调用后 bookout方法后mBook: name = null, id233
服务端:
mbook = [Book{name='null', id=0}]
判断正确:因为out是客户端无法传入参数给服务端,所以服务端的book参数都为默认值.
这时服务端修改book的id为233.然后把服务端的book传回.所以调用后客户端的name为空 id为233
AIDL基本使用4—-linkToDeath和unlinkToDeath