简体   繁体   中英

How to retrieve string data from parcel in native space?

I have a Native Command which interacts with a System Service in Android Framework through binder using parcel to fetch some data and display it to the user. The data consists of integer and string variables. The data sent from SS is sent using parcel.writeInt() and parcel.writeString() respectively for integer and string data. Now, the problem is how do I retrieve this data in native space ( not JNI though ) where my native command handling code is present in the form of a .cpp file? There is no equivalent parcel.readInt() or parcel.readString() methods in native space available to unmarshall or process the received parcel data. If I use the available methods there like parcel.readInt32() and parcel.readString16() methods, the data is incorrect ( probably ) and is not displayed on the screen. Any insight and possible solution to this problem would be much appreciated.

Thanks & Regards,

You need can create an function in Java that gets a string from the parcel:

private String getDataFromParcel(Parcel parcel) {
    return parcel.readString();
}

And call it from JNI (you said you have the parcel in the JNI context):

std::string getString(jobject object, jobject parcel) const {
    bool result = false;

    JNIEnv *env = JniGetEnv();
    jmethod jGetDataFromParcelMethod = getJMethod(object);
    jstring javaValue = (jstring) env->CallObjectMethod(object, jGetDataFromParcelMethod, parcel);

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM