#include <jni.h>
extern "C" {
jobject Java_YourPackageName_updateHashMap(JNIEnv *env, jobject thiz, jobject hashmap) {
if (!hashmap)
return hashmap;
jclass mapClass = env->GetObjectClass(hashmap);
jmethodID obj_put = env->GetMethodID(mapClass, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
if (mapClass && obj_put) {
jstring key = env->NewStringUTF("Haha");
jstring value = env->NewStringUTF("Hehe");
env->CallObjectMethod(hashmap, obj_put, key, value);
env->DeleteLocalRef(key);
env->DeleteLocalRef(value);
}
return hashmap;
}
}
Android Java code:
public native HashMap updateHashMap(HashMap<String, String>map);
static {
System.loadLibrary("native");
}
void usage() {
HashMap<String, String> mHashMap = new HashMap<String, String>();
Log.d(tag, "HashMap size: "+mHashMap.size());
updateHashMap(mHashMap);
Log.d(tag, "HashMap size: "+mHashMap.size());
Iterator it = mHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
Log.d(tag, "HashMap key: ["+pair.getKey()+"], value: ["+pair.getValue().toString()+"]");
}
}
沒有留言:
張貼留言