【每日话题】国庆放假期间的苦逼事,欢乐事!
http://www.eoeandroid.com/thread-204909-1-1.html
随手记源码啊!!忘记哪里的啦,给大家分享一下
http://www.eoeandroid.com/thread-203718-1-1.html
一个触摸界面会产生小气泡的例子
http://www.eoeandroid.com/thread-203665-1-1.html
由于每个应用程序都运行在自己的进程空间,并且可以从应用程序UI运行另一个服务进程,而且经常会在不同的进程间传递对象。在Android平台,一个进程通常不能访问另一个进程的内存空间,所以要想对话,需要将对象分解成操作系统可以理解的基本单元,并且有序的通过进程边界。
以下讲对如何实现AIDL调用进行讲解,本文包括AIDL接口调用,回调,以及传递复杂数据类型
Demo运行效果图
Aidl文件
Person.aidl package com.archermind.aidl; parcelable Person; ITaskBinder.aidl package com.archermind.aidl; import com.archermind.aidl.ITaskCallBack; import com.archermind.aidl.Person; interface ITaskBinder { void fuc01(); void fuc02(); String fuc03(in Person person); void registerCallBack(ITaskCallBack cb); void unregisterCallBack(); } ITaskCallBack.aidl package com.archermind.aidl; interface ITaskCallBack { void onActionBack(String str); }
先看Myservice的代码
服务端实现了AIDL的相关接口,供客户端调用
@Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return mBinder; } private final ITaskBinder.Stub mBinder=new ITaskBinder.Stub() { @Override public void unregisterCallBack() throws RemoteException { // TODO Auto-generated method stub Log.v(TAG,"unregisterCallBack..."); mTaskCallBack=null; } @Override public void registerCallBack(ITaskCallBack cb) throws RemoteException { // TODO Auto-generated method stub Log.v(TAG,"registerCallBack..."); mTaskCallBack=cb; } @Override public void fuc01() throws RemoteException { // TODO Auto-generated method stub Log.v(TAG,"fuc01..."); } @Override public void fuc02() throws RemoteException { // TODO Auto-generated method stub Log.v(TAG,"fuc02..."); mTaskCallBack.onActionBack("hello world"); } @Override public String fuc03(Person person) throws RemoteException { // TODO Auto-generated method stub String name=person.getName(); String descrip=person.getDescrip(); int sex=person.getSex(); String ret=""; if(sex==0){ ret="Hello "+name+",you are handsome"+"("+descrip+")"; }else{ ret="Hello "+name+",you are beautiful"+"("+descrip+")"; } return ret; } };
客户端实现回调接口
private final ITaskCallBack.Stub mCallBack=new ITaskCallBack.Stub() { @Override public void onActionBack(String str) throws RemoteException { // TODO Auto-generated method stub Log.v(TAG,"onActionBack str="+str); } };
客户端如何与服务器绑定并建立连接
Intent intent=new Intent("com.archermind.aidl.myservice"); bindService(intent,mConnection,BIND_AUTO_CREATE); private ServiceConnection mConnection=new ServiceConnection(){ @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub mService=ITaskBinder.Stub.asInterface(service); try { mService.registerCallBack(mCallBack); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } Log.v(TAG,"onServiceConnected"); }
详细信息请见附件中的源码工程,希望对大家有用.