Android: need to create a Shared Preferences object in C ++ NDK and save some Boolean value

I'm new to this, don't know how to start this,

I created a project that is connected with C ++ using Android.mk

Therefore, when I call a method from java, it should store a boolean for my general preference object.

This is my JNI method

extern "C" JNIEXPORT void JNICALL Java_com_example_sample_storeBoolean(JNIEnv *env,jobject instance){ //TODO const char *name ="hello"; __android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "***** %s *****", name); } 

The regular log I printed now works, just need to create a sharepreference object and set the value to boolean

 SharedPreferences prefs = context.getSharedPreferences("myprefdata", Context.MODE_PRIVATE); prefs.edit().putBoolean("settingnootification", true).commit(); 

Please tell me how to do this. Thanks

 public abstract SharedPreferences getSharedPreferences(String name, int mode); 

Must use this method in C ++

+8
java c ++ android android-ndk jni
source share
3 answers

I need to call the getSharedPreferences () method, since I can call it and save a boolean.

So, I created two simple methods for storing and retrieving a boolean value in C ++ NDK

MainActivity.java

 public class MainActivity extends Activity { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("Native"); } private Activity activity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activity=MainActivity.this; setStoreBoolValues(activity, true); if (getStoreValues(activity)) Log.e("Store Value", " ** true **"); else Log.e("Store Value", " ** false **"); } public native boolean getStoreValues(Activity activity); public native void setStoreBoolValues(Activity activity, boolean flag); } 

Declared a native method in MainActivity and also named my .so file

NativeClass.Cpp

 jobject mainClass; jstring spname(JNIEnv *env) { return env->NewStringUTF("sharedstore"); } jstring objectname(JNIEnv *env) { return env->NewStringUTF("boolvaluestore"); } extern "C" JNIEXPORT jboolean JNICALL Java_com_ebizzinfotech_amjad_contentresolverproj_MainActivity_getStoreValues(JNIEnv *env, jobject instance, jobject activity) { jclass spcls = env->FindClass("android/content/SharedPreferences"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getSharedPreferences", "(Ljava/lang/String;I)Landroid/content/SharedPreferences;"); jmethodID midbool = env->GetMethodID(spcls, "getBoolean", "(Ljava/lang/String;Z)Z"); jobject jobjectshared = env->CallObjectMethod(mainClass, mid, spname(env), 0); // jobject jobjectsharededit = env->CallObjectMethod(jobjectshared,midedit); jboolean jboolean1 = env->CallBooleanMethod(jobjectshared, midbool,objectname(env), JNI_FALSE); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); return jboolean1; } extern "C" JNIEXPORT void JNICALL Java_com_ebizzinfotech_amjad_contentresolverproj_MainActivity_setStoreBoolValues(JNIEnv *env, jobject instance, jobject activity, jboolean flag) { jclass spcls = env->FindClass("android/content/SharedPreferences"); jclass speditorcls = env->FindClass("android/content/SharedPreferences$Editor"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getSharedPreferences", "(Ljava/lang/String;I)Landroid/content/SharedPreferences;"); jmethodID midedit = env->GetMethodID(spcls, "edit", "()Landroid/content/SharedPreferences$Editor;"); jmethodID midputbool = env->GetMethodID(speditorcls, "putBoolean", "(Ljava/lang/String;Z)Landroid/content/SharedPreferences$Editor;"); jmethodID midapply = env->GetMethodID(speditorcls, "apply", "()V"); jobject jobjectshared = env->CallObjectMethod(mainClass, mid,spname(env), 0); jobject jobjectsharededit = env->CallObjectMethod(jobjectshared, midedit); env->CallVoidMethod(env->CallObjectMethod(jobjectsharededit, midputbool, objectname(env), flag), midapply); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); env->DeleteLocalRef(speditorcls); } 
0
source share

I just called saveBoolean(boolean bool) in MainActivity from JNI and it saved the value. Here is the code: MainActivity

 public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup. static { System.loadLibrary("native-lib"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stringFromJNI(this); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public native void stringFromJNI(MainActivity mainActivity); public void saveBoolean(boolean bool){ SharedPreferences sharedPreferences = this.getSharedPreferences("Test", Context.MODE_PRIVATE); sharedPreferences.edit().putBoolean("testBool",bool).commit(); Log.d("MainActivity","saveBoolean Called "+bool); } 

}

 #include <jni.h> #include <string> extern "C" JNIEXPORT void JNICALL Java_com_android_techgig_sharedpref_MainActivity_stringFromJNI(JNIEnv *env,jobject obj /* this */) { jclass cls = (env)->GetObjectClass(obj); //for getting class jmethodID mid = (env)->GetMethodID(cls, "saveBoolean", "(Z)V"); //for getting method signature, Z for boolean if (mid == 0) return; //will return 0 in case of class not found (env)->CallVoidMethod(obj, mid, true); //now calling actual method printf("native called"); } 

The following are the types of method signatures

 Signature Java Type Z boolean B byte C char S short I int J long F float D double 

Here is a link to learn more ..

Happy coding !!!

+3
source share

I assume that you want to call a Java class from your code. I suggest returning Java from your C ++ code.

Take a look here:

https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo032

In this example, you do the following:

  • Java calls C ++
  • Based on information passed from Java, C ++ joins the JVM
  • C ++ code calls Java code from another class (in your case it will be a SharedPreferences object)

You might need some sort of helper class to make things easier. For example. PreferencesStorer - where you get the correct preference class and pass in the values ​​you want to keep.

Take a look here:

 public static int fun() { System.out.println("From JVM"); return 0; } 

This is the method you want to call, but you want it to be: store (String value, int mode).

In your code (in Java) you need to create this object and pass it as an argument to your C ++ code. Inside C ++, you want to call a method of this object. Inside the JVM, he will already be there - ready for the challenge.

If this is not what you are looking for, I think you need to provide additional information to make the whole use case more understandable.

Good luck with JNI

+1
source share

All Articles