Android四大組件應用系列5——使用AIDL實現跨進程調用Service


一、問題描述

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

二、實現AIDL服務的步驟

1.  編寫AIDL文件

2.  如果aidl文件的內容是正確的,會自動生成一個Java接口文件(*.java)。

3.  建立一個服務類(Service的子類)。

4.  實現由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應用實現

  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>
五、客戶端實現

  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>

 

作者:傑瑞教育
出處: http://www.cnblogs.com/jerehedu/ 
版權聲明:本文版權歸 傑瑞教育 技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
技術咨詢:JRedu技術交流
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM