Android Studio 中AIDL 的創建與使用詳解

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/huangf321123/article/details/50846390

AIDL(Android Interface definition language)的縮寫,它能夠實現進程間的通信。好了,接下來,我就講解一下我使用aidl的步驟:
一:客服端
(1)新建一個工程名爲TestAidl3,如下圖所示:
名爲TestAidl3的工程
(2)新建一個序列化類CellPhone,讓它繼承Parcelable,如下圖所示:
這裏寫圖片描述
這裏寫圖片描述

(3)在當前包名下創建一個aidl文件,aidl文件名要與上面的序列化類名一致。但是這樣的話,好像創建不了aidl文件,它會提示你“Interface name must be unique”,如下圖:
這裏寫圖片描述
這時,你可以任意起一個名字,這時會生成一個aidl文件,如下圖所示:
這裏寫圖片描述
現在,你可以對這個aidl文件進行重命名,把它命名成“CellPhone”,然後在這個aidl文件裏,對這個類序列化。如下:
這裏寫圖片描述

接下來就是在當前aidl包名下,再創建一個Aidl文件,命名爲MyAidlServer,在這個文件中暴露出你想調用的方法,如圖:
這裏寫圖片描述
這裏特別要意“import com.example.administrator.testaidl3.CellPhone;”導入這個。

接下來在主activity創建一個按鈕button和textview,對button進行點擊事件,代碼如下:

package com.example.administrator.testaidl3;

import android.app.Activity;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Button mBtn;
    private TextView mTv;

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

        initView();
    }

    private void initView() {
        mBtn = (Button) findViewById(R.id.press);
        mTv = (TextView) findViewById(R.id.tv);

        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //啓動服務
                Intent intent = new Intent("com.hf.server");
                //automatically create the service as long
                //* as the binding exists.  Note that while this will create the service,
                bindService(intent,mConnection,BIND_AUTO_CREATE);
            }
        });
    }
    //服務鏈接
    ServiceConnection mConnection = new ServiceConnection() {
        String content;
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e("Test","onServiceConnected");
            //初始化aidl
            mServer = MyAidlServer.Stub.asInterface(service);

            try {
                content = mServer.call()+" 》》》》  ";
                CellPhone cell = mServer.getPhone();
                content += cell.getPhone();
                mTv.setText(content);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mServer = null;
        }
    };
}

這樣客戶端就好了。

二:服務端
創建步驟與一大致相同。如下圖:
這裏寫圖片描述
其中aidl文件內容都相同,只不過包名要注意,不要弄錯,然後還有CellPhone這個類也相同,MainActivity裏面啥都不用寫。只需要增加一個服務CellServer,代碼如下:

package com.example.administrator.testaidl5;

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

/**
 * Created by Administrator on 2016/3/10.
 */
public class CellServer extends Service{
    public CellServer(){}
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }
    MyAidlServer.Stub binder = new MyAidlServer.Stub() {
        @Override
        public String call() throws RemoteException {

            return "hello my name is call";
        }

        @Override
        public CellPhone getPhone() throws RemoteException {
            CellPhone cell = new CellPhone();
            cell.setPhone("110-1234567");
            cell.setName("公安局");
            return cell;
        }

        @Override
        public IBinder asBinder() {
            return super.asBinder();
        }

        @Override
        public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
            return super.onTransact(code, data, reply, flags);
        }
    };
}

然後記得要在文件清單裏面註冊

然後把兩個項目運行起來,點擊客戶端的按鈕,你會發現會出現錯誤,這是因爲兩個工程的包名不一致,所以,就需要把後一個工程的包名改成與第一個工程的包名一致。

最後運行起來,就能正常通訊。運行效果如下:
這裏寫圖片描述
這裏寫圖片描述
以上都是個人經歷,如果寫的不好,請大家多多體諒。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章