简体   繁体   English

Android:jni中的共享首选项

[英]Android :shared preference in jni

in my android application in need to access the shared memory between the two application ,i knew android kernel is not like Linux kernel to implement the shared memory concept ,so i have choosen the shared preference in android but i need to implement this via JNI not in java part i have tried to create a shared preference in jni but i cant get it properly this my code 在我的android应用程序中需要访问两个应用程序之间的共享内存,我知道android内核不像Linux内核那样实现共享内存概念,所以我选择了android中的共享首选项但是我需要通过JNI来实现这个在java部分我试图在jni中创建一个共享的首选项,但我不能正确地得到它我的代码

JNIEXPORT jlong JNICALL Java_com_sharedpref1_test_savevalue
  (JNIEnv *env, jobject ctx, jstring store, jstring key, jlong value);



        jobject shared_preferences = (*env)->CallObjectMethod(env, ctx, (*env)->GetMethodID(env (*env)->FindClass(env, "android/content/Context"), "getSharedPreferences", "(L/java/lang/String;I)Landroid/content/SharedPreferences;"),
                    jstore,
                    1);
        jobject preferences_editor = (*env)->CallObjectMethod(env, shared_preferences, (*env)->GetMethodID(env,
                (*env)->FindClass(env, "android/content/SharedPreferences"), "editor", "()Landroid/content/SharedPreferences$Editor;"));

        (*env)->CallObjectMethod(env, preferences_editor, (*env)->GetMethodID(env, 
                (*env)->FindClass(env, "android/content/SharedPreferences/Editor"), "putLong", "(Ljava/lang/String;J)Landroid/content/SharedPreferences$Editor;"),
               jkey,
                    value);


    }

can any know how to done shared preference in jni on android. 谁能知道如何在Android上的jni中完成共享首选项。

There's possibly an error in your code. 您的代码中可能存在错误。 "ctx" is not the Android context. “ctx”不是Android上下文。 Instead, it is the Java "this" of your calling Java class. 相反,它是您调用Java类的Java“this”。 Unless your calling class inherits Context, you should pass the Context instance it as an additional parameter. 除非您的调用类继承Context,否则应将Context实例作为附加参数传递。

Besides this, you should split and possibly error check your function calls to make it more readable and errors easier determinable. 除此之外,您应该拆分并可能错误检查函数调用,以使其更易读,更容易确定错误。 Eg 例如
Find the class. 找到这个班级。
If its valid, find the method. 如果有效,找到方法。 And so on. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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