使用Parcelable

时间:2019-04-06 10:55:09

标签: android parcelable aidl

我以这种方式创建AIDL:

我将这2个文件设置在AIDL目录中:

IMDpcService.aidl:

// IMDpcService.aidl
package amiin.bazouk.application.com.doproject;

import amiin.bazouk.application.com.doproject.MBytes;

interface IMDpcService {

    void setResetPassword(MBytes bytes);
}

MBytes.aidl:

package amiin.bazouk.application.com.doproject;

parcelable MBytes;

我在java目录中设置了这些java类:

MDpcService.java:

package amiin.bazouk.application.com.doproject;

import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

public class MDpcService extends Service {
    private final static String TAG = "Test-Tag";
    private Binder mBinder;

    @Override
    public void onCreate() {
        super.onCreate();
        mBinder = new MsiDpcServiceImpl(this);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    static class MDpcServiceImpl extends IMsiDpcService.Stub {

        private Context mContext;
        private DevicePolicyManager mDpm;
        private ComponentName cpntName;

        public MDpcServiceImpl(Context context) {
            mContext = context;
            mDpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
            cpntName = new ComponentName(context, DeviceOwnerReceiver.class);
        }

        @Override
        public setResetPassword(MBytes bytes){
            //do sth
        }
    }
}

MBytes.java

package amiin.bazouk.application.com.doproject;

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

public class MBytes implements Parcelable {
    private byte[] _byte;

    public MBytes() {
    }

    public MBytes(Parcel in) {
        readFromParcel(in);
    }


    public byte[] get_byte() {
        return _byte;
    }

    public void set_byte(byte[] _byte) {
        this._byte = _byte;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(_byte.length);
        dest.writeByteArray(_byte);
    }

    public void readFromParcel(Parcel in) {
        _byte = new byte[in.readInt()];
        in.readByteArray(_byte); 
    }

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

        public MBytes[] newArray(int size) {
            return new MBytes[size];
        }
    };

}

但是,我在编译时遇到此错误:

  

处理'命令   'C:\ Users \ Adrien \ AppData \ Local \ Android \ Sdk \ build-tools \ 27.0.3 \ aidl.exe''   以非零退出值1结束

我想念什么?

1 个答案:

答案 0 :(得分:2)

在源代码中看到了两个问题,以下发现可能会有所帮助。

1。在IMDpcService.aidl :,您需要提及方向标记( in out inout ) 指示数据去往何方。

void setResetPassword(in MBytes bytes);

方向标记的说明。

  • 输入-对象从客户端转移到仅用于输入的服务
  • out-仅将对象从客户端转移到服务 用于输出。
  • inout-对象从客户端转移到用于输入和输出的服务。

2。在MBytes.java 中,在CREATOR中指定MBytes类型,

 public static final Creator<MBytes> CREATOR
            = new Parcelable.Creator<MBytes>() {
        public MBytes createFromParcel(Parcel in) {
            return new MBytes(in);
        }

        public MBytes[] newArray(int size) {
            return new MBytes[size];
        }
    };

参考:Directional tag explanation