編輯:關於 Android 開發
AIDL(Android Interface Definition Language)是 Android 實現進程間通信(IPC)的重要機制。本文以一個簡單的「加法計算服務」為例,示範客戶端如何透過 AIDL 綁定並調用遠程 Service。
首先定義 AIDL 介面檔 CalculateInterface.aidl:
// CalculateInterface.aidl
package com.itheima.aidl.calculate;
interface CalculateInterface {
double doCalculate(double a, double b);
}
接著撰寫 CalculateService.java,在 onBind() 中返回 Stub 的實例:
package com.itheima.myaidl.server;
import com.itheima.aidl.calculate.CalculateInterface;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class CalculateService extends Service {
private final CalculateInterface.Stub mBinder = new CalculateInterface.Stub() {
@Override public double doCalculate(double a, double b) throws RemoteException {
return a + b;
}
};
@Override public IBinder onBind(Intent intent) {
Log.i("test", "onBind...");
return mBinder;
}
@Override public boolean onUnbind(Intent intent) {
Log.i("test", "onUnbind...");
return super.onUnbind(intent);
}
@Override public void onCreate() {
super.onCreate();
Log.i("test", "onCreate...");
}
@Override public void onDestroy() {
super.onDestroy();
Log.i("test", "onDestroy...");
}
}
服務端的 Manifest 需要註冊這個 Service,並設定對應的 action:
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.itheima.myaidl.server"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.itheima.myaidl.server.MainActivity"
android:configChanges="locale|layoutDirection"
android:theme="@android:style/Theme.Light.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.itheima.myaidl.server.CalculateService">
<intent-filter>
<action android:name="com.itheima.myaidl.server.CalculateService" />
</intent-filter>
</service>
</application>
</manifest>
客戶端在 onCreate 中透過 bindService 綁定遠程服務,並在 onServiceConnected 回調裡取得介面實例:
package com.itheima.myaidl.client;
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 com.itheima.aidl.calculate.CalculateInterface;
public class MainActivity extends Activity {
private CalculateInterface mService;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override public void onServiceDisconnected(ComponentName name) {
Log.i("test", "service disconnected...");
mService = null;
}
@Override public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("test", "service connected...");
mService = CalculateInterface.Stub.asInterface(service); // 獲取介面實例
}
};
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 綁定遠程服務
Intent intent = new Intent();
intent.setAction("com.itheima.myaidl.server.CalculateService");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
try {
double result = mService.doCalculate(1, 2);
Log.i("test", "result===>" + result);
} catch (RemoteException e) {
e.printStackTrace();
}
}
super.onWindowFocusChanged(hasFocus);
}
@Override protected void onDestroy() {
unbindService(mServiceConnection); // 解綁遠程服務
super.onDestroy();
}
}
運行結果截圖如下:

透過這個例子不難看出,AIDL 的核心在於「定義介面 → 服務端實現 Stub → 客戶端 asInterface 轉換 → 綁定調用」這條主線,掌握後就能輕鬆實現跨進程的方法調用。
給 Android GridView 畫上分割線:兩種思路的完整實現
給 GridView 畫分割線 ListView 想加分割線很簡單,布局裡寫上 android:divider 和 android:dividerHeight 就完事。可
Android 自定義視圖:雪花與下雨動畫效果
Android 雪花與下雨效果 透過自定義 View 與 Canvas 繪製,可以在 Android 中實作出漫天飛舞的雪花或下雨天的動畫效果。這類效果常用於節日主題、娛
Android Studio 安裝 Genymotion 插件與常見問題
Android Studio 安裝 Genymotion 插件 Genymotion 是常用的 Android 模擬器之一,效能與操作體驗通常優於部分內建模擬器。本文說明
FFmpeg 使用手冊:Windows、Linux 與 macOS 平台編譯安裝
為什麼需要自行編譯 FFmpeg FFmpeg 官方提供了可執行文件,許多 Linux 發行版源中也包含 FFmpeg 包。但系統源中的版本往往較舊,可能不支持新的編碼格