Android : Need to create Shared Preferences object in c++ NDK and Store some Boolean value
I just called saveBoolean(boolean bool)
in MainActivity from JNI and it saved the value.
Here is 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");
}
Here is method signatures types
Signature Java Type
Z boolean
B byte
C char
S short
I int
J long
F float
D double
Here is link to explore more..
Happy coding!!!