我正在做一个安卓应用,需要追加5-6个jstrings,并希望在本地代码中返回一个jstring。字符串就像...
jstring1= "Hello, "
jstring2= "How"
jstring3= "are"
jstring4= "you"
jstring5= "today?"
我想返回一个jstring,比如 "你好,你今天怎么样?" 我只是想知道最简单的方法。
首先,你要做一个 StringBuilder
:
jclass cls_StringBuilder = env->FindClass("java/lang/StringBuilder");
jmethodID ctr_StringBuilder = env->GetMethodID(cls_StringBuilder, "<init>", "(I)V");
jobject stringBuilder = env->NewObject(cls_StringBuilder, ctr_StringBuilder, 100);
然后你再一个字符串一个字符串地追加。
jmethodID mid_StringBuilder_append = env->GetMethodID(cls_StringBuilder, "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
for (auto str: { jstring1, jstring2, jstring3, jstring4, jstring5 }) {
env->CallObjectMethod(stringBuilder, mid_StringBuilder_append, str);
}
最后抓住StringBuilder的内容。
jmethodID mid_StringBuilder_toString = env->GetMethodID(cls_StringBuilder, "toString", "()Ljava/lang/String;");
jstring final = (jstring) env->CallObjectMethod(stringBuilder, mid_StringBuilder_toString);
return final;