获取枚举字段
jclass c = env->FindClass("android/graphics/Bitmap$Config");
jfieldID id = env->GetStaticFieldID(c,"ARGB_8888", "Landroid/graphics/Bitmap$Config;");
jobject o = env->GetStaticObjectField(c, id);
调用构造函数
jclass c = env->FindClass(env, "org/stone/WebView");
jmethodID m = jni->GetMethodID(c, "<init>", "(JLjava/lang/String;Landroid/content/Context;II)V");
jobject o = env->NewObject(c, m , reinterpret_cast<intptr_t>(this), env->NewStringUTF("ThreadName", j_content, width, height));
调用静态方法
jclass c = env->FindClass(env, "org/stone/Utility");
jmethodID m = jni->GetStaticMethodID(c, "DrawBitmap", "(JLandroid/graphics/Bitmap;Landroid/os/Handler;Landroid/view/View;)V");
env->CallStaticVoidMethod(c, m, reinterpret_cast<intptr_t>(this), j_bitmap, j_handler, j_view);
调用对象方法
jmethodID m = jni->GetMethodID(c, "goForward","()V");;
env->CallVoidMethod(o, m);
Java String转换为std::string
std::string JavaToStdString(JNIEnv* jni, const jstring& j_string)
{
const jclass string_class = GetObjectClass(jni, j_string);
const jmethodID get_bytes = GetMethodID(jni, string_class, "getBytes", "(Ljava/lang/String;)[B");
const jstring charset_name = jni->NewStringUTF("ISO-8859-1");
const jbyteArray j_byte_array = (jbyteArray)jni->CallObjectMethod(j_string, get_bytes, charset_name);
const size_t len = jni->GetArrayLength(j_byte_array);
std::vector<char> buf(len);
jni->GetByteArrayRegion(j_byte_array, 0, len, reinterpret_cast<jbyte*>(&buf[0]));
return std::string(buf.begin(), buf.end());
}
std::map转换为Java HashMap
jobject StdMapStringToJava(std::map<std::string, std::string> additional_http_headers)
{
jclass c = jni->FindClass("java/util/HashMap");
jobject o = jni->NewObject(c, jni->GetMethodID(c, "<init>", "()V"));
jmethodID m = jni->GetMethodID(c, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
for (const auto& it : additional_http_headers) {
jni->CallObjectMethod(o, m, jni->NewStringUTF(it.first.c_str()), jni->NewStringUTF(it.second.c_str()));
}
return o;
}
Java Map转换为std::map
std::map<std::string, std::string> JavaToStdMapStrings(JNIEnv* jni, jobject j_map)
{
jclass map_class = jni->FindClass("java/util/Map");
jclass set_class = jni->FindClass("java/util/Set");
jclass iterator_class = jni->FindClass("java/util/Iterator");
jclass entry_class = jni->FindClass("java/util/Map$Entry");
jmethodID entry_set_method = jni->GetMethodID(map_class, "entrySet", "()Ljava/util/Set;");
jmethodID iterator_method = jni->GetMethodID(set_class, "iterator", "()Ljava/util/Iterator;");
jmethodID has_next_method = jni->GetMethodID(iterator_class, "hasNext", "()Z");
jmethodID next_method = jni->GetMethodID(iterator_class, "next", "()Ljava/lang/Object;");
jmethodID get_key_method = jni->GetMethodID(entry_class, "getKey", "()Ljava/lang/Object;");
jmethodID get_value_method = jni->GetMethodID(entry_class, "getValue", "()Ljava/lang/Object;");
jobject j_entry_set = jni->CallObjectMethod(j_map, entry_set_method);
jobject j_iterator = jni->CallObjectMethod(j_entry_set, iterator_method);
std::map<std::string, std::string> result;
while (jni->CallBooleanMethod(j_iterator, has_next_method)) {
jobject j_entry = jni->CallObjectMethod(j_iterator, next_method);
jstring j_key = static_cast<jstring>(jni->CallObjectMethod(j_entry, get_key_method));
jstring j_value = static_cast<jstring>(jni->CallObjectMethod(j_entry, get_value_method));
result[JavaToStdString(jni, j_key)] = JavaToStdString(jni, j_value);
}
return result;
}
来源:CSDN
作者:StoneLiuXS
链接:https://blog.csdn.net/momo0853/article/details/103977445