Android入門:通過AIDL進行處理序間通訊

來源:互聯網
上載者:User
一、AIDL介紹AIDL:Android Interface Definition Language,介面定義語言;顧名思義,就是定義介面的語言,即利用AIDL可以定義介面;
AIDL簡單地說就是處理序間通訊的方法,類似於Java中的RMI;
AIDL利用Xxx.aidl檔案定義介面,通常將此檔案放在com.xiazdong.aidl中;
AIDL檔案的編寫類似於介面的編寫,但有一些不同:
(1)AIDL中的介面和方法前不能加上任何存取權限修飾符(如public、private、static、final);(2)AIDL支援Java基本類型(3)AIDL中的方法參數如果是非Java基本類型,則要加上in、out、inout;(4)AIDL對於自訂類型,則需要實現Parselable介面;
AIDL編寫後會在gen目錄中自動產生java檔案;
在自動產生的檔案中我們需要瞭解的方法有:(1)Xxx xx = Xxx.stub.asInterface(IBinder);
//將IBinder類型轉換成AIDL類型,在ServiceConnection的onServiceConnected中使用;(2)Xxx.stub.fun();
//調用aidl定義的方法

需要注意的是,由於AIDL檔案是兩應用通訊的橋樑,因此此檔案在兩應用中都要存在;
二、代碼案例

情境說明:此情境和“Android入門:綁定本地服務”文章中的應用功能一樣,不同的是加法的服務在AIDLService應用,而調用加法的程式在AIDLClient應用中;
效果和以前一樣:



程式組織圖的不同:



AIDLClient代碼

AddOp.aidl
package com.xiazdong.aidl;interface AddOp {//除了沒有存取權限,其他和介面定義差不多int addService(int a,int b);}

MainActivity.java


package com.xiazdong.aidlclient;import com.xiazdong.aidl.AddOp;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private EditText op1,op2;private Button button;private TextView result;private AddOp binder ;private ServiceConnection conn = new AddOpServiceConnection();private OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View v) {int number = 0;try {number = binder.addService(Integer.parseInt(op1.getText().toString()), Integer.parseInt(op2.getText().toString()));} catch (Exception e) {e.printStackTrace();}result.setText("result="+number+"");}};@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        op1 = (EditText)this.findViewById(R.id.op1);        op2 = (EditText)this.findViewById(R.id.op2);        result = (TextView)this.findViewById(R.id.result);        button = (Button)this.findViewById(R.id.button);        button.setOnClickListener(listener);        Intent service = new Intent("com.xiazdong.action");//隱式意圖        this.bindService(service, conn, BIND_AUTO_CREATE);    }private class AddOpServiceConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) { binder = AddOp.Stub.asInterface(service);//將IBinder轉換成介面類型}@Overridepublic void onServiceDisconnected(ComponentName name) {binder = null;}}}


AIDLService代碼

AddOp.aidl

package com.xiazdong.aidl;interface AddOp {int addService(int a,int b);}
AddOpService.java
package com.xiazdong.aidlservice;import com.xiazdong.aidl.AddOp;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class AddOpService extends Service {private IBinder binder = new AddOpBinder();@Overridepublic IBinder onBind(Intent intent) {return binder;}public int add(int a,int b){//服務提供的方法通過Binder對象提供給外部return a+b;}private class AddOpBinder extends AddOp.Stub{//繼承aidl產生的存根@Overridepublic int addService(int a, int b) throws RemoteException {//實現此方法return add(a,b);//調用服務的方法}}}






相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.