android aidl 传递parcelable

一.截图

android aidl 传递parcelable_第1张图片android aidl 传递parcelable_第2张图片

二.服务端

1.新建aidl文件夹

2.新建aidl文件(因为传递的是自定义类型,所以必须写个以类型命名的aidl文件)

注意:a.这里必须导入自定义类型,要不然不识别

          b.形参必须指定in或者out

IMyAidlInterface.aidl
// IMyAidlInterface.aidl
package com.caomingyu.parcelableserver;

import com.caomingyu.parcelableserver.Person;

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

interface IMyAidlInterface {
    List addPerson(in Person person);
}
Person.aidl
// Person.aidl
package com.caomingyu.parcelableserver;

// Declare any non-default types here with import statements
parcelable Person;

3.写自定义实体类

package com.caomingyu.parcelableserver;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by caomingyu on 2018/11/20.
 */

public class Person implements Parcelable {
    private String name;
    private int age;

    public Person(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

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

    @Override
    public String toString() {
        return "{ name = " + name + " , age = " + age + "}";
    }
}

4.新建service文件

IRemoteService.java
package com.caomingyu.parcelableserver;

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

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

/**
 * Created by caomingyu on 2018/11/20.
 */

public class IRemoteService extends Service {

    private List personList;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        personList = new ArrayList();
        return iBinder;
    }

    private IBinder iBinder = new IMyAidlInterface.Stub() {
        @Override
        public List addPerson(Person person) throws RemoteException {
            Log.d("personList", "addPerson: " + personList.toString());
            personList.add(person);
            return personList;
        }
    };
}

5.在mainactivity文件里启动该service

package com.caomingyu.parcelableserver;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intent = new Intent(MainActivity.this, IRemoteService.class);
        startService(intent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(intent);
    }
}

6.在manifest里面注册IRemoteService




    
        
            
                

                
            
        
        
    

三.客户端

1.把服务端的两个aidl文件拷贝过来,跟服务端内容和包名必须一致

2.布局文件写好




    

3.把服务端自定义Person类拷贝下来放到与服务到一致的包名下

4.在mainactivity实现与服务端的远程通信

package com.caomingyu.parcelableclient;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.caomingyu.parcelableserver.IMyAidlInterface;
import com.caomingyu.parcelableserver.Person;

public class MainActivity extends AppCompatActivity {

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

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

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

    private void bindService() {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.caomingyu.parcelableserver","com.caomingyu.parcelableserver.IRemoteService");
        intent.setComponent(componentName);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    private void initView() {
        mBtAddPerson = (Button) findViewById(R.id.bt_add_person);
        mBtAddPerson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    iMyAidlInterface.addPerson(new Person("lisan",12));
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

server端github地址: https://github.com/bruse1990/AidlParcelable

client端github地址:https://github.com/bruse1990/ParcelableClient

你可能感兴趣的:(android)