AIDL使用

一.新建一个项目AIDLservice,创建Person类并继承Parcelable接口。

public class Person implements Parcelable {
     
    private String name;
    private int grade;
    public Person(String name ,int grade){
     
        this.name = name;
        this.grade = grade;
    }
    protected Person(Parcel in){
     
        this.name = in.readString();
        this.grade = in.readInt();
    }
    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);
    }
}

二.创建Person.aidl

     只有将类在AIDL中声明后,AIDL才能调用该Person类。
AIDL使用_第1张图片
三.创建MyAidl.aidl(作用:设置让客户端允许调用的接口)

AIDL使用_第2张图片

四.创建AidlService.java并注册这个Service

public class AidlService extends Service {
     
    private ArrayList<Person> persons;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
     
        persons = new ArrayList<>();
        Log.e("AidlService","success onBind");
        return iBinder;
    }

    private IBinder iBinder = new Myaidl.Stub() {
     
        @Override
        public void addPerson(Person person) throws RemoteException {
     
            persons.add(person);
        }

        @Override
        public List<Person> getPersonList() throws RemoteException {
     
            return persons;
        }
    };

    @Override
    public void onCreate() {
     
        super.onCreate();
        Log.e("AidlService","onCreate success");
    }
}

     因为服务端的Service需要被客户端来远程绑定,所以客户端要能够找到这个Service,可以通过先指定包名,之后再配置Action值或者直接指定Service类名的方式来绑定Service。本文将在客户端通过指定Action值的方式来绑定Service,所以需要将Service在项目AIDLclient的AndroidManifest.xml中进行如下注册:

<service android:name=".AidlService"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="myAidlService"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
 </service>

     此时服务端的代码就写完了,下面来写客户端的代码。
五.再次新建一个项目AIDLclient,把项目AIDLservice中的aidl那个文件夹复制到项目AIDLclient中,把项目AIDLservice中的Person.java复制到项目AIDLclient中,注意这两次复制时要保证包名不能改变,保证是原封不动的复制。
AIDL使用_第3张图片
六.直接修改MainActivity的代码:

public class MainActivity extends AppCompatActivity {
     

    private final String TAG = "AIDLclient";
    private Myaidl myAidl;
    private Button btn;
    private boolean connected;

    private ServiceConnection connection = new ServiceConnection() {
     
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
     
            Log.e(TAG,"onServiceConnected success");
            myAidl = Myaidl.Stub.asInterface(service);
            connected = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
     
            Log.e(TAG,"onServiceDisconnected success");
            myAidl = null;
            connected = false;
        }
    };

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

    private void initView(){
     
        btn = findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
     
            @Override
            public void onClick(View v) {
     
                try {
     
                    if(connected){
     
                        myAidl.addPerson(new Person("Van",59));
                        List<Person> persons = myAidl.getPersonList();
                        Log.e(TAG,persons.toString());
                    }
                    else {
     
                        Log.e(TAG,"未连接");
                    }
                }catch (Exception e){
     
                    e.printStackTrace();
                }
            }
        });
    }

    private void bindService(){
     
        Intent intent = new Intent();
//        intent.setComponent(new ComponentName("com.example.aidlservice","com.example.aidlservice.AidlService"));
        intent.setPackage("com.example.aidlservice");
        intent.setAction("myAidlService");
        bindService(intent,connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
     
        super.onDestroy();
        if(connected)
            unbindService(connection);
    }
}

七.结果
     先启动服务APP,再启动客户端APP,当客户端APP启动后,服务端APP的日志如下:
在这里插入图片描述点击客户端按钮后客户端APP日志如下:
在这里插入图片描述

你可能感兴趣的:(Android,android,android,studio)