android binder ipc

来源:互联网 发布:北京阿里云可用区a 编辑:程序博客网 时间:2024/06/08 11:13

服务端

IDateTimeService.aidl 文件写法 与普通java类相同 

package com.example.visualizertest;interface IDateTimeService { String getCurrentDateTime(in String format) ;}

 没问题的话 会在Gen目录下生成 同名的.java文件

/* * This file is auto-generated.  DO NOT MODIFY. * Original file: D:\\ProjectGitReposit\\VisualizerTest\\src\\com\\example\\visualizertest\\IDateTimeService.aidl */package com.example.visualizertest;public interface IDateTimeService extends android.os.IInterface{/** Local-side IPC implementation stub class. */public static abstract class Stub extends android.os.Binder implements com.example.visualizertest.IDateTimeService{private static final java.lang.String DESCRIPTOR = "com.example.visualizertest.IDateTimeService";/** Construct the stub at attach it to the interface. */public Stub(){this.attachInterface(this, DESCRIPTOR);}/** * Cast an IBinder object into an com.example.visualizertest.IDateTimeService interface, * generating a proxy if needed. */public static com.example.visualizertest.IDateTimeService asInterface(android.os.IBinder obj){if ((obj==null)) {return null;}android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);if (((iin!=null)&&(iin instanceof com.example.visualizertest.IDateTimeService))) {return ((com.example.visualizertest.IDateTimeService)iin);}return new com.example.visualizertest.IDateTimeService.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_getCurrentDateTime:{data.enforceInterface(DESCRIPTOR);java.lang.String _arg0;_arg0 = data.readString();java.lang.String _result = this.getCurrentDateTime(_arg0);reply.writeNoException();reply.writeString(_result);return true;}}return super.onTransact(code, data, reply, flags);}private static class Proxy implements com.example.visualizertest.IDateTimeService{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 getCurrentDateTime(java.lang.String format) 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.writeString(format);mRemote.transact(Stub.TRANSACTION_getCurrentDateTime, _data, _reply, 0);_reply.readException();_result = _reply.readString();}finally {_reply.recycle();_data.recycle();}return _result;}}static final int TRANSACTION_getCurrentDateTime = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);}public java.lang.String getCurrentDateTime(java.lang.String format) throws android.os.RemoteException;}

写个Service 
package com.example.visualizertest;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class DateTimeService extends Service {public IBinder onBind(Intent arg0) {return new IDateTimeService.Stub() {public String getCurrentDateTime(String format) throws RemoteException {return new SimpleDateFormat(format).format(new Date());}};}}

配置文件中配置

<service android:name=".DateTimeService" >            <intent-filter>                <action android:name="com.example.visualizertest.DateTimeService">                </action>            </intent-filter>        </service>

客户端 :

创建一个与服务端 aidl相同的包名 相同的aidl文件 gen下自动生成.java文件与服务端相同。
否则会报ERROR/AndroidRuntime(716): java.lang.SecurityException: Binder invocation to an incorrect interface这个错误

使用service与在同进程 相同
package com.example.binder;import android.app.Activity;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.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.example.visualizertest.IDateTimeService;public class MainActivity2 extends Activity {class MyServiceConnection implements ServiceConnection {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {printLog("bind success");is=IDateTimeService.Stub.asInterface(service);}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}TextView tv1;Button b1;Button b2;MyServiceConnection sc;IDateTimeService is;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv1 = (TextView) findViewById(R.id.textView1);b1 = (Button) findViewById(R.id.button1);b2 = (Button) findViewById(R.id.button2);b1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent("com.example.visualizertest.DateTimeService");sc = new MyServiceConnection();bindService(intent, sc, BIND_AUTO_CREATE);}});b2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {String s=is.getCurrentDateTime("yyyy-MM-hh");printLog("return-->"+s);} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}@Overrideprotected void onDestroy() {unbindService(sc);super.onDestroy();}private void printLog(String string) {Log.d("MainActivity2", string);}}

结:aidl接口文件相同

原创粉丝点击