Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
一、問題描述
Android應(yīng)用程序的四大組件中Activity、BroadcastReceiver、ContentProvider、Service都可以進(jìn)行跨進(jìn)程。在上一篇我們通過ContentProvider實(shí)現(xiàn)了不同應(yīng)用之間的跨進(jìn)程調(diào)用,但ContentProvider主要是提供數(shù)據(jù)的共享(如sqlite數(shù)據(jù)庫),那么我們希望跨進(jìn)程調(diào)用服務(wù)(Service)呢?Android系統(tǒng)采用了遠(yuǎn)程過程調(diào)用(RPC)方式來實(shí)現(xiàn)。與很多其他的基于RPC的解決方案一樣,Android使用一種接口定義語言(Interface Definition Language,IDL)來公開服務(wù)的接口。對(duì)于Service的跨進(jìn)程調(diào)用需要通過AIDL來實(shí)現(xiàn),AIDL服務(wù)應(yīng)用非常廣泛,如百度地圖API中,就提供跨進(jìn)程的服務(wù),下面我們就看看如何實(shí)現(xiàn)AIDL Service ,案例如圖:

二、實(shí)現(xiàn)AIDL服務(wù)的步驟
1. 編寫AIDL文件
2. 如果aidl文件的內(nèi)容是正確的,會(huì)自動(dòng)生成一個(gè)Java接口文件(*.java)。
3. 建立一個(gè)服務(wù)類(Service的子類)。
4. 實(shí)現(xiàn)由aidl文件生成的Java接口。
5. 在AndroidManifest.xml文件中配置AIDL服務(wù), 添加<action>標(biāo)簽的android:name,以便客戶端使用隱式Intent啟動(dòng)服務(wù)
6、客戶端
三、編寫AIDL文件
Android接口定義語言——LocalService.aidl
package com.jereh.remote;
interface LocalService{
String getLocal();
}
IDE會(huì)自動(dòng)生成LocalService.java 文件 如圖所示:

四、Remote應(yīng)用實(shí)現(xiàn)
1、編寫MyRemoteService
public class MyRemoteService extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return new MyRemoteServiceImpl();
}
private class MyRemoteServiceImpl extends LocalService.Stub{
@Override
public String getLocal() throws RemoteException {
// TODO Auto-generated method stub
return "煙臺(tái)杰瑞教育";
}
}
}
2、AndroidManifest.xml配置
<service android:name="com.jereh.retmote.MyRemoteService"
android:process="remote"
>
<intent-filter>
<action android:name="com.jereh.remote_service"/>
</intent-filter>
</service>
五、客戶端實(shí)現(xiàn)
1、在客戶端應(yīng)用中添加LocalService.aidl
注意包名要與文件的在服務(wù)端定義的包名相同。如圖所示:

同樣會(huì)自動(dòng)生成LocalService.java 代碼
2、MainActivity代碼:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view){
Intent service=new Intent("com.jereh.remote_service");
super.bindService(service, conn, Context.BIND_AUTO_CREATE);
}
public void showInfo(View view){
try {
local=service.getLocal();
Log.d("jereh", local);
Toast.makeText(this,
"您已進(jìn)入"+local,Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private LocalService service;
private String local;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
// TODO Auto-generated method stub
// 獲取遠(yuǎn)程Service的onBinder方法返回的對(duì)象代理
service=LocalService.Stub.asInterface(binder);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
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"
tools:context=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="啟動(dòng)遠(yuǎn)程服務(wù)" android:onClick="startService" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看信息" android:onClick="showInfo" />
</LinearLayout>
以上所述就是本文給大家介紹的Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service,希望大家喜歡。
相關(guān)文章
android中實(shí)現(xiàn)在ImageView上隨意畫線涂鴉的方法
今天小編就為大家分享一篇android中實(shí)現(xiàn)在ImageView上隨意畫線涂鴉的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Android實(shí)現(xiàn)的數(shù)字格式化用法示例
這篇文章主要介紹了Android實(shí)現(xiàn)的數(shù)字格式化用法,結(jié)合實(shí)例形式分析了Android數(shù)學(xué)運(yùn)算中數(shù)字格式化輸出的相關(guān)技巧,需要的朋友可以參考下2016-08-08
Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-02-02
Android應(yīng)用創(chuàng)建多個(gè)快捷方式
本文主要介紹Android 生成多個(gè)快捷方式,這里提供代碼實(shí)例,詳細(xì)講解生成多個(gè)快捷方式的實(shí)現(xiàn)方法,有需要的朋友可以參考下2016-07-07
Android延遲實(shí)現(xiàn)的幾種解決方法及原理分析
這篇文章主要給大家介紹了關(guān)于Android延遲實(shí)現(xiàn)的幾種解決方法以及其中的原理分析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
淺談Android設(shè)計(jì)模式學(xué)習(xí)之觀察者模式
觀察者模式在實(shí)際項(xiàng)目中使用的也是非常頻繁的,它最常用的地方是GUI系統(tǒng)、訂閱——發(fā)布系統(tǒng)等。這篇文章主要介紹了淺談Android設(shè)計(jì)模式學(xué)習(xí)之觀察者模式,感興趣的小伙伴們可以參考一下2018-05-05
Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法
這篇文章主要介紹了Android實(shí)現(xiàn)簡(jiǎn)單圖片壓縮的方法,詳細(xì)分析了Android針對(duì)圖片的讀取、縮放及保存等操作技巧,需要的朋友可以參考下2016-06-06

