IPC機制 : AIDL的用法

HeiMcMullan 8年前發布 | 7K 次閱讀 AIDL Android開發 移動開發

(AS開發工具)

服務器端

第一步 :創建aidl文件

  • 在與java文件夾同級目錄下,創建文件夾 aidl ,然后在aidl文件夾下創建包 com.lyp.aidl ,再在包下創建 IMyAidlInterface.aidl 文件

Paste_Image.png

// IMyAidlInterface.aidl
package com.lyp.aidl;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
    /**
     * 1.接口和方法前不用加private等訪問權限的修飾符,當然也不能使用final,static修飾符
     * 2.接口名稱和aidl的名稱要一致
     * 3.aidl默認支持的類型包話java基本類型(int、long、boolean)和(String、List、Map、CharSequence),
     *   使用這些類型時不需要import聲明。對于List和Map中的元素類型必須是Aidl支持的類型。如果使用自定義類型
     *   作為參數或返回值,自定義類型必須實現Parcelable接口
     * 4.自定義類型和AIDL生成的其它接口類型在aidl描述文件中,應該顯式import,即便在該類和定義的包在同一個包中
     * 5.在aidl文件中所有非Java基本類型參數必須加上in、out、inout標記,以指明參數是輸入參數、輸出參數還是輸入輸出參數
     */
    int add(int num1, int num2);
}
  • 使用as工具欄Build/make project ,重新編譯 ,生成與aidl文件同名的java文件

Paste_Image.png

第二步 : 完成服務器端Server類

package com.lyp.aidlserver;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;

import com.lyp.aidl.IMyAidlInterface;


/**
 * Created by ${思格斯} on 2016/10/8 0008.
 */
public class AidlService extends Service {
    public AidlService() {
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    //獲取IMyAidlInterface.Stub對象,實現IMyAidlInterface.java中的add方法
    private IBinder mBinder= new IMyAidlInterface.Stub() {
        @Override
        public int add(int num1, int num2) throws RemoteException {
            return num1+num2;
        }
    };
}

客戶端

第一步 : 將服務器端的aidl文件夾復制一份到客戶端

Paste_Image.png

第二步 : 綁定服務端Service , 獲取代理對象 ,調用aidl文件自動定義的方法

package com.lyp.aidlclient;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lyp.aidl.IMyAidlInterface;

public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Button mButton;

    private IMyAidlInterface mBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.text);
        mButton= (Button) findViewById(R.id.btn);

        bindService();

        //調用服務端方法
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    int res = mBinder.add(1, 1);
                    mTextView.setText(String.valueOf(res));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //顯式綁定服務
    private void bindService() {
        Intent intent = new Intent("android.intent.action.AIDLSERVICE");
        intent.setPackage("com.lyp.aidlserver");
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //獲取遠程服務代理
            mBinder = IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //釋放資源
            mBinder = null;
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //解綁服務
        unbindService(conn);
    }
}

 

來自:http://www.jianshu.com/p/e84bdb4cf28a

 

 本文由用戶 HeiMcMullan 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!