天天看點

Android中 AIDL 實際開發步驟

AIDL基本知識點

定義: Android 接口定義語言。

作用:不同應用的用戶端通過 IPC 方式通路服務,并且希望在服務中進行多線程處理時,您才有必要使用 AIDL

官方文檔:Android 接口定義語言 (AIDL)  |  Android 開發者  |  Android Developers (google.cn)

Android中 AIDL 實際開發步驟

https://developer.android.google.cn/guide/components/aidl?hl=zh-cn

操作步驟

第一步:建立 aidl 檔案
Android中 AIDL 實際開發步驟
Android中 AIDL 實際開發步驟
第二步:定義接口 
package com.wust.aidltestdemo;

interface AidlTestAction {
       void firstAidlTest();
}           
Android中 AIDL 實際開發步驟
第三步:建構項目

原因:因為 aidl 檔案所寫的接口是由編輯器在編譯我們的項目時自動幫我們生成的接口,是以,我們想要引用,那必須得先建構項目。

Android中 AIDL 實際開發步驟
第四步:編寫服務 引用我們編寫的 aidl 接口 

在這裡,我先給大家示範  aidl 接口  在自己内部應用中的使用,但這并不是他的核心任務!!!

  • 編寫接口實作類
package com.wust.aidltestdemo;

import android.os.RemoteException;
import android.util.Log;

/**
 * ClassName: AidlTestActionImpl <br/>
 * Description: <br/>
 * date: 2021/10/25 21:58<br/>
 *
 * @author yiqi<br />
 * @QQ 1820762465
 * @微信 yiqiideallife
 * @技術交流QQ群 928023749
 */
public class AidlTestActionImpl extends AidlTestAction.Stub {

    private String TAG = "AidlTestActionImpl";

    @Override
    public void firstAidlTest() {
        Log.d(TAG, "firstAidlTest is executed");
    }
}
           

我們可以看到  AidlTestAction.Stub 類繼承自 android.os.Binder,也就是說,我們這個實作類繼承自 AidlTestAction.Stub 後就可以 在服務類的 onBinder() 方法中傳回

Android中 AIDL 實際開發步驟
  • 編寫服務類
package com.wust.aidltestdemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import androidx.annotation.Nullable;

/**
 * ClassName: AidlService <br/>
 * Description: <br/>
 * date: 2021/10/25 21:56<br/>
 *
 * @author yiqi<br />
 * @QQ 1820762465
 * @微信 yiqiideallife
 * @技術交流QQ群 928023749
 */
public class AidlService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new AidlTestActionImpl();
    }
}
           
  • 綁定服務

在清單檔案中聲明

<service android:name=".AidlService" />           

綁定服務邏輯編寫

package com.wust.aidltestdemo;

import androidx.appcompat.app.AppCompatActivity;

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;

public class MainActivity extends AppCompatActivity {

    private MyServiceConnect myServiceConnect;
    private boolean isBind;
    private AidlTestAction mAidlTestAction;

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

        Intent intent = new Intent(this, AidlService.class);
        myServiceConnect = new MyServiceConnect();
        isBind = bindService(intent, myServiceConnect, BIND_AUTO_CREATE);
    }

    private class MyServiceConnect implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //這裡你也可以寫
//            AidlTestActionImpl aidlTestAction = (AidlTestActionImpl) service;
            mAidlTestAction = AidlTestAction.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    public void test(View view) {
        try {
            mAidlTestAction.firstAidlTest();
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myServiceConnect != null && isBind) {
            unbindService(myServiceConnect);
            isBind = false;
        }
    }
}           

布局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="AIDL 測試"
        android:onClick="test"/>

</LinearLayout>