標籤:
AIDL即 Android Interface Definition Language。原因:On Android, one process cannot normally access thememory of another process.
也就是說AIDL用於android處理序間通訊,下面就記錄一下第一個aidl的demo。
官方文檔也給出了基本的使用方法,如:
1. 在android項目的src相關包下建立一個aidl檔案 IRemoteService.aidl:
package com.example.testndk;interface IRemoteService { /** Request the process ID of this service, to do evil things with it. */ int getPid(); /** Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);}建立成功後會在gen目錄自動產生一個同名的java檔案,開啟可以看到其實是一個介面
public interface IRemoteService extends android.os.IInterface
2. 實現這個介面
package com.example.testndk;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.Process;public class RemoteService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface return mBinder; } private final IRemoteService.Stub mBinder = new IRemoteService.Stub() { public int getPid(){ return Process.myPid(); } public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) { // Does nothing } };}
3. 把介面暴露給用戶端,即調用處
package com.example.testndk;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;import android.widget.TextView;public class TestNdkActivity extends Activity { private static final String TAG = "TestNdkActivity"; private TextView tv1 = null; // static { // System.loadLibrary("TestNdk"); // } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // // String str = JniClient.AddStr("prefix", "suffix"); // // // int iSum = JniClient.AddInt(5, 2); // String strSum = "5 + 2 = " + iSum; tv1 = new TextView(this); bindService(new Intent(RemoteService.class.getName()), mConnection, Context.BIND_AUTO_CREATE); setContentView(tv1); } IRemoteService mIRemoteService = null; private ServiceConnection mConnection = new ServiceConnection() { // Called when the connection with the service is established public void onServiceConnected(ComponentName className, IBinder service) { // Following the example above for an AIDL interface, // this gets an instance of the IRemoteInterface, which we can use // to call on the service mIRemoteService = IRemoteService.Stub.asInterface(service); try { tv1.setText("mIRemoteService=" + mIRemoteService.getPid()); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // Called when the connection with the service disconnects unexpectedly public void onServiceDisconnected(ComponentName className) { Log.e(TAG, "Service has unexpectedly disconnected"); mIRemoteService = null; } }; protected void onDestroy() { super.onDestroy(); unbindService(mConnection); }}
用到了service,還需在AndroidManifest.xml配置一下,添加
<service android:name="com.example.testndk.RemoteService"> <intent-filter> <action android:name="com.example.testndk.RemoteService"/> </intent-filter> </service>
因為是通過action來尋找service的,所以action標籤不可少。
官方文檔的例子稍微麻煩一點,不過也是比較清楚的。
Android進程通訊:AIDL入門執行個體