1.新建IRemoteService.aidl
package com.tang.remoteservicedemo; interface IRemoteService { String getInfo(); }从内容中也可以看出,这东西类似一个接口。既然定义了这么一个玩意,那么我们就要去实现它。
2.新建IService“实现”IRemoteService“接口”
package com.tang.remoteservicedemo; import java.util.Date; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class IService extends Service { private IBinder iBinder = new IRemoteService.Stub() { @Override public String getInfo() throws RemoteException { // TODO Auto-generated method stub return new Date(System.currentTimeMillis()).toLocaleString()+" 来自远程服务的信息!"; } }; @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return iBinder; } }基于Binder的不同进程间通信,Client与Service在不同的进程中,对用户程序而言当调用Service返回的IBinder接口后,访问Service中的方法就如同调用自己的函数一样。
3.配置IService供其他进程调用
<service android:name="com.tang.remoteservicedemo.IService" android:process=":remote"> <intent-filter> <action android:name="com.tang.remoteservicedemo.IService"/> </intent-filter> </service>
通过前面三个步骤,这个含有getInfo()方法的service就可以给别人调用了,下面在客户端调用它
4.新建一个ClientDemo工程将含有IRemoteService.aidl的那个包全部拷贝到src下,只留下aidl文件,其他全删除。
5.新建MainActivity,其他就是绑定service的操作了
package com.tang.clientdemo; import java.util.Timer; import java.util.TimerTask; import com.tang.remoteservicedemo.IRemoteService; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class MainActivity extends Activity { private IRemoteService iService = null; private boolean isBinded =false; ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub isBinded = false; iService = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub iService = IRemoteService.Stub.asInterface(service); isBinded = true; } }; public void doBind() { Intent intent = new Intent("com.tang.remoteservicedemo.IService"); bindService(intent, conn, Context.BIND_AUTO_CREATE); } public void doUnbind() { if (isBinded) { unbindService(conn); iService = null; isBinded = false; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub doBind(); } }).start(); Timer timer = new Timer(); timer.schedule(task, 0, 2000); } TimerTask task = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub if(iService!=null) { try { Log.i("AAA", iService.getInfo()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Log.i("AAA", "iService!=null"); } } }; @Override protected void onDestroy() { // TODO Auto-generated method stub doUnbind(); task.cancel(); super.onDestroy(); } }
为什么会有一个为空的Log呢,因为绑定也是要时间的嘛....
简单的AIDL的使用就这么多了
原文地址:http://blog.csdn.net/tangnengwu/article/details/40824539