进程之间的通信AIDL

 

远程端:

package cn.itcast.aidl;
//AIDL
//首先建立AIDL文件,有点和接口类似,建立好AIDL文件后,
//会在gen文件夹下自动生成用于远程通信的类
//文件的后缀为aidl
interface StudentQuery {

    String queryStudent(int number);
}

自动生成的用于远程通信的类(gen文件夹下)

package cn.itcast.aidl;
//AIDL

public interface StudentQuery extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements cn.itcast.aidl.StudentQuery
{
private static final java.lang.String DESCRIPTOR = "cn.itcast.aidl.StudentQuery";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
 * Cast an IBinder object into an cn.itcast.aidl.StudentQuery interface,
 * generating a proxy if needed.
 */
public static cn.itcast.aidl.StudentQuery asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof cn.itcast.aidl.StudentQuery))) {
return ((cn.itcast.aidl.StudentQuery)iin);
}
return new cn.itcast.aidl.StudentQuery.Stub.Proxy(obj);
}
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_queryStudent:
{
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
java.lang.String _result = this.queryStudent(_arg0);
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements cn.itcast.aidl.StudentQuery
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
public java.lang.String queryStudent(int number) 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);
_data.writeInt(number);
mRemote.transact(Stub.TRANSACTION_queryStudent, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_queryStudent = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
public java.lang.String queryStudent(int number) throws android.os.RemoteException;
}


服务中对远程通信的类的使用

public class StudentQueryService extends Service {
    private String[] names = {"张飞", "李静", "赵薇"};
    private IBinder binder = new StudentQueryBinder();
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    private String query(int number){
        if(number > 0 && number < 4){
            return names[number - 1];
        }
        return null;
    }
    private final class StudentQueryBinder extends StudentQuery.Stub{
        public String queryStudent(int number) throws RemoteException {            
            return query(number);
        }        
    }

以下是客户端:

1、客户端也要建立相应的远程通信的aidl文件。

2、对远程服务的调用

public class MainActivity extends Activity {
    private EditText numberText;
    private TextView resultView;
    private StudentQuery studentQuery;
    private StudentConnection conn = new StudentConnection();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        numberText = (EditText) this.findViewById(R.id.number);
        resultView = (TextView) this.findViewById(R.id.resultView);
        Intent service = new Intent("cn.itcast.student.query");
        bindService(service, conn, BIND_AUTO_CREATE);
    }

    public void queryStudent(View v) {
        String number = numberText.getText().toString();
        int num = Integer.valueOf(number);
        try {
            resultView.setText(studentQuery.queryStudent(num));
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(conn);
        super.onDestroy();
    }

    private final class StudentConnection implements ServiceConnection {
        public void onServiceConnected(ComponentName name, IBinder service) {
            studentQuery = StudentQuery.Stub.asInterface(service);
        }
        public void onServiceDisconnected(ComponentName name) {
            studentQuery = null;
        }
    }
}

 

 

posted @ 2014-01-12 11:38  天之涯0204  阅读(306)  评论(0编辑  收藏  举报