目錄
Android跨進(jìn)程通信之小例子(一)
Android跨進(jìn)程通信之非AIDL(二)
Android跨進(jìn)程通信之Proxy與Stub(三)
Android跨進(jìn)程通信之AIDL(四)
什么是AIDL
從該系列的幾篇文章里丹锹,我們知道了
Proxy和Stub模式
。其實跨進(jìn)程通信
中所謂的AIDL就是幫我們?nèi)崿F(xiàn)Proxy和Stub模式
讲竿,幫我們封裝好編碼
和譯碼
功能真朗。底層還是transact
和onTransact
方法的調(diào)用摆马。
小例子
做一個經(jīng)典的Echo程序茫船。程序向服務(wù)發(fā)送一句話,服務(wù)給打印出來纯命。
第一步:定義AIDL接口文件(提供Service的APP)
你的服務(wù)提供哪些接口去讓別人使用愿题,你要先說清楚损俭,但是此時并不需要給出實現(xiàn)。
- 新建
com.example.aidlechoservice.aidl
包 - 新建一個普通文件抠忘,命名為
IEchoService.aidl
package com.example.aidlechoservice.aidl;
//這里是當(dāng)前文件所在包名
interface IEchoService{
String echo(String inStr);
}
如果使用Eclipse的話撩炊,這樣定義之后我們會看到產(chǎn)生了
gen/com.example.aidlechoservice.aidl/IEchoService.java
文件
第二步:實現(xiàn)服務(wù)端的Stub(提供Service的APP)
超級簡單,就一句話崎脉。
IEchoService.Stub mBinder = new IEchoService.Stub() {
@Override
public String echo(String inStr) throws RemoteException {
return "echo " + inStr + " at " + sdf.format(new Date());
}
};
所以整體代碼就這樣
public class EchoService extends Service {
private IEchoService.Stub mBinder;
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void onCreate() {
super.onCreate();
mBinder = new IEchoService.Stub() {
@Override
public String echo(String inStr) throws RemoteException {
return "echo " + inStr + " at " + sdf.format(new Date());
}
};
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
}
第三步:編寫APP的AIDL(調(diào)用Service的APP)
跟上面服務(wù)的是一模一樣拧咳,必須是一模一樣,否則就不行囚灼。
第四步:實現(xiàn)客戶端的Proxy(調(diào)用Service的APP)
很簡單骆膝,還是一句話搞定
IEchoService mService = IEchoService.Stub.asInterface(binder);
布局
<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" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="bind Service" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="echo"
android:text="echo" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="unbind"
android:text="unbind Service" />
</LinearLayout>
代碼
public class MainActivity extends Activity {
private IEchoService mService;
private ServiceConnection mServiceConnection;
private Intent mServiceIntent = new Intent();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mServiceIntent.setComponent(
new ComponentName("com.example.aidlechoservice", "com.example.aidlechoservice.service.EchoService"));
mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
mService = IEchoService.Stub.asInterface(binder);
}
};
}
public void bind(View v) {
if (isBinded()) {
return;
}
bindService(mServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
public void unbind(View v) {
if (!isBinded()) {
return;
}
unbindService(mServiceConnection);
}
public void echo(View v) {
if (!isBinded()) {
return;
}
try {
String result = mService.echo("Hello world!!!");
Log.i("TAG", result);
} catch (RemoteException e) {
e.printStackTrace();
}
}
private boolean isBinded() {
return mService != null;
}
}