Android中兩個應(yīng)用之間使用AIDL進行通信

AIDL的全稱,Android Interface Definition Language。簡單來說,AIDL的作用是讓你可以在自己的APP里綁定一個其他APP的service。這樣一個APP可以和另一個APP交互。

代碼實現(xiàn)說明

兩個應(yīng)用之間通信。先創(chuàng)建兩個工程。

首先第一個

工程名稱OneProject,新建一個aidl文件。


image.png

內(nèi)容如下

package com.example.myapplication;

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

interface IMyAidlInterface {
    /**
    此方法可以忽略。這是說明aidl可傳遞的基本類型
     */
    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
            double aDouble, String aString);
//定義一個傳遞方法
String getName();
}

在項目中新建一個服務(wù)(service),注意,在onBind中返回一個自定義的Binder類,MyBinder繼承IMyAidlInterface.Stub,然后重寫里邊的方法getName返回要傳遞的信息數(shù)據(jù)。

package com.example.myapplication.service;

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

import com.example.myapplication.IMyAidlInterface;

public class MyService extends Service {
    public MyService() {
    }

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

    class MyBinder extends IMyAidlInterface.Stub{

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

        }

        @Override
        public String getName() throws RemoteException {
            return "test,數(shù)據(jù)傳遞顯示";
        }
    }

}

修改AndroidManifest.xml,添加該service,并在intent-filter中添加action標(biāo)簽,客戶端將通過該標(biāo)簽綁定該服務(wù)。


image.png

到這里第一個項目的準(zhǔn)備工作完成。

接下來第二個項目

創(chuàng)建第二個工程TwoProject,將第一個項目中的整個aidl文件夾復(fù)制過來,要保證目錄結(jié)構(gòu)一致。確保所有使用的aidl文件都復(fù)制過來并且目錄一致。簡單說兩個項目都用到的東西要各一份一模一樣,如果要是自定義的類型,那就aidl文件以及自己實現(xiàn)的序列化類型。


image.png

接下來是MainActivity中的代碼,綁定服務(wù)。

public class MainActivity extends AppCompatActivity {


    private Button button;
    private IMyAidlInterface iMyAidlInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.btn_tost);

        Intent intent=new Intent();
        intent.setAction("com.example.myapplication.service.MyService");
        intent.setPackage("com.example.myapplication");
        bindService(intent,conn,BIND_AUTO_CREATE);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Log.i("deede",iMyAidlInterface.getName());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

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

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}

Intent完成遠程啟動service,如果是Android版本高于5.0.選擇隱式啟動遠程service、需要設(shè)置package,注意的是這個package是第一個項目的包名。

語法和Java語法的細微區(qū)別

1,不能用private,public,protect修飾方法
2,支持傳遞Java的基本數(shù)據(jù)類型,(byte、short、int、long、float、double、char、boolean),String,CharSequence,List(接受端必須是ArrayList),Map(接收端必須是HashMap),其他自定義的類型需要實現(xiàn)Parcelable序列化。
3,Aidl定義的接口和實現(xiàn)Parcelable序列化的類必須import,即使在相同的包結(jié)構(gòu)下,其余的類型不需要import
4,對于基本數(shù)據(jù)類型,也不是String和CharSequence類型的,需要有方向指示,包括in(表示由客戶端設(shè)置),out(表示由服務(wù)端設(shè)置),inout,(表示兩者均可設(shè)置)

參考鏈接如下
https://blog.csdn.net/songkai0825/article/details/89345042

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

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

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