【原创】Android aidl实现两个apk之间远程调用Service
Aidl,android平台的IPC方式之一,基于系统的Ibinder机制。
网上大多数例子都是在一个apk下来测试调用service,现在我在两个project下面来调用。
一个是server project,一个是client project
首先我们建立的是server project,这里面要实现aidl文件和一个service,activity只是用来启动service的,当然,你也可以通过发广播的形式来启动service。
首先看IAidlService.aidl文件:
package com.ds.server;interface IAidlService { int getType(); }
package com.ds.server;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import android.util.Log;public class AidlService extends Service {private IAidlService.Stub mBinder = new IAidlService.Stub() {@Overridepublic int getType() throws RemoteException {// TODO Auto-generated method stubreturn 5;}};private void Log(String str) { Log.d("AidlService", "------ " + str + "------");}@Overridepublic void onCreate() {Log("service create"); }@Overridepublic void onStart(Intent intent, int startId) {Log("service start id=" + startId);}@Overridepublic IBinder onBind(Intent t) {Log("service on bind");return mBinder;}@Overridepublic void onDestroy() {Log("service on destroy");super.onDestroy();}@Overridepublic boolean onUnbind(Intent intent) {Log("service on unbind");return super.onUnbind(intent);}public void onRebind(Intent intent) {Log("service on rebind");super.onRebind(intent);}}
<service android:name=".AidlService" android:enabled="true" android:process=":remote" > <intent-filter> <action android:name="com.ds.server.IAidlService" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
package com.ds.client;import com.ds.server.IAidlService;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;public class AidlClientActivity extends Activity {IAidlService iservice; private ServiceConnection connection = new ServiceConnection() {public void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stub// 从远程service中获得AIDL实例化对象iservice = IAidlService.Stub.asInterface(service);Log.i("Client","Bind Success:" + iservice);} public void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubiservice = null;Log.i("Client","onServiceDisconnected");}}; /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.main);final TextView tv = (TextView) findViewById(R.id.tv);Button bt = (Button) findViewById(R.id.bt);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubIntent service = new Intent(IAidlService.class.getName());bindService(service, connection, BIND_AUTO_CREATE);if (iservice != null) { try {tv.setText("" + iservice.getType());} catch (RemoteException e) {e.printStackTrace();}}}});}}