AIDL (Android Interface Definition Language) 是一种IDL 语言,用于生成可以在Android设备上两个进程之间进行进程间通信(interprocess communication, IPC)的代码。
AndroidManifest.xml
仰慕者应用:
AndroidManifest.xml
如果在一个进程中(例如Activity)要调用另一个进程中(例如Service)对象的操作,就可以使用AIDL生成可序列化的参数。
例子:仰慕者要对一个女神表白
这是一个女神服务:
package com.lzb.romate.service;
import java.util.Random;
import com.lzb.aidl.Vindicate;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
public class GoddessService extends Service {
String[] attitude = {"我也喜欢你","抱歉我不喜欢你","抱歉我已经有喜欢的人了"};
IBinder binder = new IBinderGoddess();
@Override
public IBinder onBind(Intent arg0) {
return binder; //返回一个binder,让仰慕者有一个机会来表达(为了能调用speakNow方法)
}
private final class IBinderGoddess extends Vindicate.Stub{
@Override
public String speakNow(String name) throws RemoteException {
int maybe = (int)(new Random().nextInt(3));
if("男神".equals(name)){
return attitude[0];
}else{
return attitude[maybe];
}
}
}
}
Vindicate.aidl
package com.lzb.aidl;
interface Vindicate {
String speakNow(String name); //爱的告白
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lzb.goddess"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="com.lzb.romate.service.GoddessService" >
<intent-filter>
<action android:name="com.lzb.sayloveyou" />
</intent-filter>
</service>
</application>
</manifest>
仰慕者应用:
MainActivity.java
package com.lzb.client.admirer;
import com.lzb.aidl.Vindicate;
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.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText etsay;
private TextView tvresult;
private ServiceConnection conn = new AdmireServiceConn();
private Vindicate vindicate;
Intent service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etsay = (EditText)this.findViewById(R.id.etsay);
tvresult = (TextView)this.findViewById(R.id.tvresult);
service = new Intent("com.lzb.sayloveyou"); //通过隐式意图调用女神的服务
bindService(service, conn, Context.BIND_AUTO_CREATE); //绑定服务
}
public void who(View v){
String name = etsay.getText().toString();
try {
tvresult.setText(vindicate.speakNow(name));
} catch (RemoteException e) {
e.printStackTrace();
}
}
private class AdmireServiceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
vindicate = Vindicate.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
vindicate = null;
}
}
@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}
}
AIDL代码:把上面的包和代码一并复制过来(注意两个aidl的包名一定要一样)
界面:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lzb.client.admirer.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/who" />
<EditText
android:id="@+id/etsay"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="who"
android:text="@string/btnsubmit" />
<TextView
android:id="@+id/tvresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.lzb.client.admirer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">仰慕者</string>
<string name="who">我是一位</string>
<string name="action_settings">Settings</string>
<string name="btnsubmit">提交</string>
</resources>