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

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

四、Remote應用實現(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 "煙臺杰瑞教育";
}
}
}
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>
五、客戶端實現(xiàn)
1、在客戶端應用中添加LocalService.aidl
注意包名要與文件的在服務端定義的包名相同。如圖所示:

同樣會自動生成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,
"您已進入"+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
// 獲取遠程Service的onBinder方法返回的對象代理
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="啟動遠程服務" android:onClick="startService" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看信息" android:onClick="showInfo" />
</LinearLayout>
以上所述就是本文給大家介紹的Android應用程序四大組件之使用AIDL如何實現(xiàn)跨進程調(diào)用Service,希望大家喜歡。
相關文章
android中實現(xiàn)在ImageView上隨意畫線涂鴉的方法
今天小編就為大家分享一篇android中實現(xiàn)在ImageView上隨意畫線涂鴉的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Android實現(xiàn)的數(shù)字格式化用法示例
這篇文章主要介紹了Android實現(xiàn)的數(shù)字格式化用法,結合實例形式分析了Android數(shù)學運算中數(shù)字格式化輸出的相關技巧,需要的朋友可以參考下2016-08-08
Android編程實現(xiàn)EditText字數(shù)監(jiān)聽并顯示的方法
這篇文章主要介紹了Android編程實現(xiàn)EditText字數(shù)監(jiān)聽并顯示的方法,涉及Android EditText文本框事件監(jiān)聽與響應相關操作技巧,需要的朋友可以參考下2017-02-02

