Android Studio 中AIDL 的创建与使用详解

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);
        }
    };
}

然后记得要在文件清单里面注册

然后把两个项目运行起来,点击客户端的按钮,你会发现会出现错误,这是因为两个工程的包名不一致,所以,就需要把后一个工程的包名改成与第一个工程的包名一致。

最后运行起来,就能正常通讯。运行效果如下:
这里写图片描述
这里写图片描述
以上都是个人经历,如果写的不好,请大家多多体谅。

猜你喜欢

转载自blog.csdn.net/huangf321123/article/details/50846390