Android — AIDL開發(fā)記錄(一)

AIDL是指Android Interface definition language(接口定義語言),主要用于進(jìn)程間的通信。

創(chuàng)建aidl文件

首先需要在和java同級的目錄中建一個aidl文件夾,然后右鍵new出aidl文件。

image.png

新建的aidl文件默認(rèn)會創(chuàng)建一個方法,告訴我們支持的基礎(chǔ)類型有哪些

interface IMyAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
}

aidl的簡單使用

使用基礎(chǔ)類型,創(chuàng)建一個方法getName

interface IMyAidlInterface {
   
   String getName(int age);
   
   void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
           double aDouble, String aString);
}

因?yàn)閍idl是用于進(jìn)程間通信的,所以需要把這個aidl拷貝到另一個進(jìn)程中(可以新建工程),需要保證兩個進(jìn)程中aidl文件的包名一致,不然會報錯。

rebuild項(xiàng)目后,會發(fā)現(xiàn)自動生成了相應(yīng)的java接口


image.png

服務(wù)端進(jìn)程創(chuàng)建Service和返回Binder

public class MyService extends Service {

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

    class MyBinder extends IMyAidlInterface.Stub {

        @Override
        public String getName(int age) throws RemoteException {
            return age > 30 ? "老王" : "小王";
        }

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString)
                throws RemoteException {

        }
    }

}

在AndroidManifest.xml中注冊Service

<service android:name=".MyService">
    <intent-filter>
        <actaion android:name="com.xfz.aidlapplication.MyService123" />
    </intent-filter>
</service>

客戶端創(chuàng)建ServiceConnection并綁定服務(wù)

首先創(chuàng)建intent,

  • 可以使用Component,傳入包名和服務(wù)類名
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.xfz.aidlapplication", "com.xfz.aidlapplication.MyService"));
  • 也可以使用Action和Package
Intent intent = new Intent();
intent.setAction("com.xfz.aidlapplication.MyService123");
intent.setPackage("com.xfz.aidlapplication");

綁定和調(diào)用

public class MainActivity extends AppCompatActivity {

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

        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.xfz.aidlapplication", "com.xfz.aidlapplication.MyService"));
//        intent.setAction("com.xfz.aidlapplication.MyService123");
//        intent.setPackage("com.xfz.aidlapplication");
        bindService(intent, mConnection, Service.BIND_AUTO_CREATE);

    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //在服務(wù)連接成功后進(jìn)行調(diào)用
            IMyAidlInterface mIntentionPlus = IMyAidlInterface.Stub.asInterface(service);
            try {
                Log.d("xfz", "name = " + mIntentionPlus.getName(35));
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容