AIDL的简单使用

第一步:创建自己的AIDL文件

package com.example.viewbindingdemo;
import com.example.viewbindingdemo.Person;
import java.util.List;

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

interface ITestAidlInterface {
    /**
     * Demonstrates some basic types that you can use as parameters
     * and return values in AIDL.
     */
  List<Person> getPerson();
  void setPersonNum(int  num);
  void setPerson(in Person person);

}

在接口中声名自己的方法,供客户端使用。并在该路径下创建实体类映射aidl文件。

 

第二步:在客户端创建实体类Person并实现Parcelable  接口,以便序列化与反序列化

/**
 * Create by Angisy
 * on 2020/4/8
 */
public class Person implements Parcelable {
    private String name;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", uid='" + uid + '\'' +
                '}';
    }

    public Person(String name, String id) {
        this.name = name;
        this.uid = id;
    }


    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    private String uid;

    protected Person(Parcel in) {
        name = in.readString();
        uid = in.readString();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(uid);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

第三步:make Project 生成binder文件

 

第四步:创建服务端,创建Service,实现生成的binder对象的实例,实现接口定义的方法,在onBind 中返回

package com.example.viewbindingdemo;

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

import com.example.viewbindingdemo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * Create by Angisy
 * on 2020/4/8
 */
public class RemoteService extends Service {
    private static final String TAG = "RemoteService";
    ArrayList<Person> list = new ArrayList<>();
    IBinder iBinder = new ITestAidlInterface.Stub() {
        @Override
        public List<Person> getPerson() throws RemoteException {
            ToastUtils.toastLight("getPerson is done ");
            return list;
        }

        @Override
        public void setPersonNum(int num) throws RemoteException {
            ToastUtils.toastLight("serPersonNum is done ");
            Log.d(TAG,"num:"+ num);
        }

        @Override
        public void setPerson(Person person) throws RemoteException {
            Log.d(TAG,"person:"+ person);
            ToastUtils.toastLight("setPerson is done ");
            list.add(person);
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG,"onbind is done ");
        return iBinder;
    }
}

第五步:在客户端创建ServiceConnection的实例,获取AIDL的对象,bindService,

package com.example.viewbindingdemo;

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.TextView;


import java.util.List;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    private ITestAidlInterface iTestAidlInterface1;
    private ServiceConnection serviceConnection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        TextView setNum = findViewById(R.id.set_num);
        TextView setPerson = findViewById(R.id.set_person);
        TextView getPerson = findViewById(R.id.get_person);
        setPerson.setOnClickListener(this);
        setNum.setOnClickListener(this);
        getPerson.setOnClickListener(this);
        Intent intent = new Intent(this,RemoteService.class);
        initData();
        bindService(intent,serviceConnection,BIND_AUTO_CREATE);
    }


    private void initData() {
        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                iTestAidlInterface1 = ITestAidlInterface.Stub.asInterface(service);
            }

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

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.set_person:
                try {
                    iTestAidlInterface1.setPerson(new Person("奥巴马","USA"));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }

                break;

            case R.id.set_num:
                try {
                    iTestAidlInterface1.setPersonNum(12);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;

            case R.id.get_person:
                List<Person> person = null;
                try {
                    person = iTestAidlInterface1.getPerson();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                Log.d(TAG,"person:"+ person);
                break;
        }

    }
}

附demo链接:https://download.csdn.net/download/acMiner/12320685 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值