Android應(yīng)用程序四大組件之使用AIDL如何實(shí)現(xiàn)跨進(jìn)程調(diào)用Service
一、問(wèn)題描述
Android應(yīng)用程序的四大組件中Activity、BroadcastReceiver、ContentProvider、Service都可以進(jìn)行跨進(jìn)程。在上一篇我們通過(guò)ContentProvider實(shí)現(xiàn)了不同應(yīng)用之間的跨進(jìn)程調(diào)用,但ContentProvider主要是提供數(shù)據(jù)的共享(如sqlite數(shù)據(jù)庫(kù)),那么我們希望跨進(jìn)程調(diào)用服務(wù)(Service)呢?Android系統(tǒng)采用了遠(yuǎn)程過(guò)程調(diào)用(RPC)方式來(lái)實(shí)現(xiàn)。與很多其他的基于RPC的解決方案一樣,Android使用一種接口定義語(yǔ)言(Interface Definition Language,IDL)來(lái)公開(kāi)服務(wù)的接口。對(duì)于Service的跨進(jìn)程調(diào)用需要通過(guò)AIDL來(lái)實(shí)現(xiàn),AIDL服務(wù)應(yīng)用非常廣泛,如百度地圖API中,就提供跨進(jìn)程的服務(wù),下面我們就看看如何實(shí)現(xiàn)AIDL Service ,案例如圖:
二、實(shí)現(xiàn)AIDL服務(wù)的步驟
1. 編寫(xiě)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、客戶端
三、編寫(xiě)AIDL文件
Android接口定義語(yǔ)言——LocalService.aidl
package com.jereh.remote; interface LocalService{ String getLocal(); }
IDE會(huì)自動(dòng)生成LocalService.java 文件 如圖所示:
四、Remote應(yīng)用實(shí)現(xiàn)
1、編寫(xiě)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)文章
加載頁(yè)面遮擋耗時(shí)操作任務(wù)頁(yè)面--第三方開(kāi)源之AndroidProgressLayout
AndroidProgressLayout實(shí)現(xiàn)為界面添加圓形進(jìn)度條。調(diào)用setprogress()方法顯示和隱藏進(jìn)度條,這篇文章主要介紹了加載頁(yè)面遮擋耗時(shí)操作任務(wù)頁(yè)面--第三方開(kāi)源之AndroidProgressLayout的相關(guān)資料,需要的朋友可以參考下2015-11-11Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球,隨著手指觸摸移動(dòng)而移動(dòng)的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10如何判斷軟件程序是否聯(lián)網(wǎng) 聯(lián)網(wǎng)狀態(tài)提示信息Android實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何判斷軟件程序是否聯(lián)網(wǎng)的實(shí)現(xiàn)代碼,Android實(shí)現(xiàn)聯(lián)網(wǎng)狀態(tài)信息提示,感興趣的小伙伴們可以參考一下2016-05-05Android 中ViewPager重排序與更新實(shí)例詳解
這篇文章主要介紹了Android 中ViewPager重排序與更新實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07Android啟動(dòng)初始化方案App StartUp的應(yīng)用詳解
這篇文章主要介紹了Android啟動(dòng)初始化方案App StartUp的使用方法,StartUp是為了App的啟動(dòng)提供的一套簡(jiǎn)單、高效的初始化方案,下面我們來(lái)詳細(xì)了解2022-09-09Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)計(jì)步進(jìn)度的環(huán)形Progress,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02