天天看點

Android AIDL 使用詳解

AIDL

  AIDL (Android Interface Definition Language),Android接口定義語言,Android提供的IPC (Inter Process Communication,程序間通信)的一種獨特實作。

為什麼使用AIDL

  使用AIDL隻有在你允許來自不同應用的用戶端跨程序通信通路你的service,并且想要在你的service種處理多線程的時候才是必要的。 如果你不需要執行不同應用之間的IPC并發,你應該通過實作Binder建立你的接口,或者如果你想執行IPC,但是不需要處理多線程。那麼使用Messenger實作你的接口。

定義AIDL接口的步驟

1、建立.aidl 檔案(Android Studio)

Android AIDL 使用詳解
// IMyAidlInterface.aidl
package com.owen.provider;

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

interface IMyAidlInterface {
    String getValue();
}
           

2、實作接口并暴露給用戶端

Android SDk工具基于.aidl檔案使用java語言生成一個接口 這個接口有一個内部抽象類,叫做Stub,它是繼承Binder并且實作你AIDL接口的 你必須繼承這個Stub類并且實作這些方法,實作一個service并且覆寫onBind()方法傳回你的Stub實作類。

public class UpdateService extends Service {

    private String aidl = "aidl";

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


    private class MyServiceImp extends IMyAidlInterface.Stub {

        @Override
        public String getValue() throws RemoteException {
            return aidl;
        }
    }
}
           

實作步驟 :

1、aidl

interface IMyAidlInterface {
    String getValue();
}
           

2、建立服務類

public class UpdateService extends Service {

    private String aidl = "aidl";

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

    private class MyServiceImp extends IMyAidlInterface.Stub {

        @Override
        public String getValue() throws RemoteException {
            return aidl;
        }
    }
}
           

3、AndroidManifest中申明Service

<service android:name=".UpdateService">
    <intent-filter>
        <!--  指定調用AIDL服務的ID -->
        <action android:name="com.owen.provider.IMyAidlInterface" />
    </intent-filter>
</service>
           

4、啟動Service

public class MainActivity extends AppCompatActivity {

    private IMyAidlInterface iMyAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindService(new Intent("com.owen.provider.IMyAidlInterface"), new Connection(), Context.BIND_AUTO_CREATE);
        findViewById(R.id.aidl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (iMyAidlInterface != null) {
                        Toast.makeText(MainActivity.this, iMyAidlInterface.getValue(), Toast.LENGTH_LONG).show();
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private class Connection implements ServiceConnection {


        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidlInterface =  IMyAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}