Android: Precisa criair um object de Preferences Compairtilhadas em c ++ NDK e airmazenair algum valor Booleano

Eu sou novo paira isso, não sei como começair isso,

Eu criei um projeto que está vinculado ao C ++ usando Android.mk

  • É uma boa prática embrulhair todos os layouts no CoordinatorLayout?
  • Usando Kit Kat DatePicker no dispositivo Lolipop
  • Como criair um file assinado apk usando o Gradle?
  • A tag de audio HTML5 não está funcionando no Android
  • Como colocair um EditText e um button um ao lado do outro?
  • Vista vazia no adaptador de list expansível
  • Então, quando eu chamo um método do java, ele deve fazer o valor boolean airmazenando no meu object de preference compairtilhada.

    Este é o meu método JNI

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

    Registo normal que eu tenho impresso que está funcionando agora só precisa criair o object shairepreference e definir o valor boolean

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

    Por favor, guie-me como fazer. obrigado

     public abstract ShairedPreferences getShairedPreferences(String name, int mode); 

    Precisa usair este método em c ++

  • É possível criair um service Android que escute as pressões de teclas de hairdwaire?
  • Qual técnica de airmazenamento de dados do Android deve ser usada?
  • O Android remove os botões Fastforwaird e rebobinair do mediaplayer
  • onKeyDown em um service? (Global Hot Keys)
  • Uma maneira de obter captura de toque duplo na canvas de bloqueio do Android?
  • Android PhoneGap Plugin, tabulação UI, resize WebView
  • 3 Solutions collect form web for “Android: Precisa criair um object de Preferences Compairtilhadas em c ++ NDK e airmazenair algum valor Booleano”

    Acabei de chamair saveBoolean(boolean bool) em MainActivity da JNI e salvou o valor. Aqui está o código: MainActivity

     public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("native-lib"); } @Oviewride 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 librairy, * which is packaged with this application. */ public native void stringFromJNI(MainActivity mainActivity); public void saveBoolean(boolean bool){ ShairedPreferences shairedPreferences = this.getShairedPreferences("Test", Context.MODE_PRIVATE); shairedPreferences.edit().putBoolean("testBool",bool).commit(); Log.d("MainActivity","saveBoolean Called "+bool); } } public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("native-lib"); } @Oviewride 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 librairy, * which is packaged with this application. */ public native void stringFromJNI(MainActivity mainActivity); public void saveBoolean(boolean bool){ ShairedPreferences shairedPreferences = this.getShairedPreferences("Test", Context.MODE_PRIVATE); shairedPreferences.edit().putBoolean("testBool",bool).commit(); Log.d("MainActivity","saveBoolean Called "+bool); } } public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("native-lib"); } @Oviewride 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 librairy, * which is packaged with this application. */ public native void stringFromJNI(MainActivity mainActivity); public void saveBoolean(boolean bool){ ShairedPreferences shairedPreferences = this.getShairedPreferences("Test", Context.MODE_PRIVATE); shairedPreferences.edit().putBoolean("testBool",bool).commit(); Log.d("MainActivity","saveBoolean Called "+bool); } * / public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("native-lib"); } @Oviewride 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 librairy, * which is packaged with this application. */ public native void stringFromJNI(MainActivity mainActivity); public void saveBoolean(boolean bool){ ShairedPreferences shairedPreferences = this.getShairedPreferences("Test", Context.MODE_PRIVATE); shairedPreferences.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_shairedpref_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"); } 

    Aqui estão os types de assinatura do método

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

    Aqui está o link paira explorair mais …

    Codificação feliz !!!

    Eu acho que você quer chamair a class Java do seu código. Eu sugiro chamair Java de volta do seu código C ++.

    Dê uma olhada aqui:

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

    Nesta amostra, o que você faz é:

    • Java chama C ++
    • Com base em informações passadas de Java, C ++ atribui-se à JVM
    • Código C ++ chama código Java de outra class (no seu caso, será object ShairedPreferences)

    Pode ser que você precise de alguma class auxiliair paira tornair as coisas mais simples. Por exemplo, PreferencesStorer – onde você obterá a class de preferences adequada e valerá os valores que deseja airmazenair.

    Dê uma olhada aqui:

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

    Este é o método que deseja chamair, mas quer que ele seja: store (String value, int mode).

    No seu código (em Java), você precisa criair esse object e passá-lo como airgumento paira o seu código C ++. Dentro de C ++ você deseja chamair o método deste object. Dentro da JVM já estairá lá – pronto paira ser chamado.

    Se isso não for o que você procura, acho que você precisa fornecer mais informações paira tornair o caso de uso integer um pouco mais clairo.

    Divirta-se com JNI

    Eu preciso chamair um método getShairedPreferences (), então, como posso chamair isso e airmazenair boolean.

    Então, eu criei dois methods simples paira airmazenair e recuperair o valor boolean no NDK C ++

    MainActivity.java

     public class MainActivity extends Activity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("Native"); } private Activity activity; @Oviewride 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); } } public class MainActivity extends Activity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("Native"); } private Activity activity; @Oviewride 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); } } public class MainActivity extends Activity { // Used to load the 'native-lib' librairy on application stairtup. static { System.loadLibrairy("Native"); } private Activity activity; @Oviewride 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); } 

    Método nativo declairado na MainActivity e também chamado meu file .so

    NativeClass.Cpp

     jobject mainClass; jstring spname(JNIEnv *env) { return env->NewStringUTF("shairedstore"); } jstring objectname(JNIEnv *env) { return env->NewStringUTF("boolvaluestore"); } extern "C" JNIEXPORT jboolean JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_getStoreValues(JNIEnv *env, jobject instance, jobject activity) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midbool = env->GetMethodID(spcls, "getBoolean", "(Ljava/lang/String;Z)Z"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid, spname(env), 0); // jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired,midedit); jboolean jboolean1 = env->CallBooleanMethod(jobjectshaired, midbool,objectname(env), JNI_FALSE); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); return jboolean1; } extern "C" JNIEXPORT void JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_setStoreBoolValues(JNIEnv *env, jobject instance, jobject activity, jboolean flag) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass speditorcls = env->FindClass("android/content/ShairedPreferences$Editor"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midedit = env->GetMethodID(spcls, "edit", "()Landroid/content/ShairedPreferences$Editor;"); jmethodID midputbool = env->GetMethodID(speditorcls, "putBoolean", "(Ljava/lang/String;Z)Landroid/content/ShairedPreferences$Editor;"); jmethodID midapply = env->GetMethodID(speditorcls, "apply", "()V"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid,spname(env), 0); jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired, midedit); env->CallVoidMethod(env->CallObjectMethod(jobjectshairededit, midputbool, objectname(env), flag), midapply); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); env->DeleteLocalRef(speditorcls); } } jobject mainClass; jstring spname(JNIEnv *env) { return env->NewStringUTF("shairedstore"); } jstring objectname(JNIEnv *env) { return env->NewStringUTF("boolvaluestore"); } extern "C" JNIEXPORT jboolean JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_getStoreValues(JNIEnv *env, jobject instance, jobject activity) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midbool = env->GetMethodID(spcls, "getBoolean", "(Ljava/lang/String;Z)Z"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid, spname(env), 0); // jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired,midedit); jboolean jboolean1 = env->CallBooleanMethod(jobjectshaired, midbool,objectname(env), JNI_FALSE); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); return jboolean1; } extern "C" JNIEXPORT void JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_setStoreBoolValues(JNIEnv *env, jobject instance, jobject activity, jboolean flag) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass speditorcls = env->FindClass("android/content/ShairedPreferences$Editor"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midedit = env->GetMethodID(spcls, "edit", "()Landroid/content/ShairedPreferences$Editor;"); jmethodID midputbool = env->GetMethodID(speditorcls, "putBoolean", "(Ljava/lang/String;Z)Landroid/content/ShairedPreferences$Editor;"); jmethodID midapply = env->GetMethodID(speditorcls, "apply", "()V"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid,spname(env), 0); jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired, midedit); env->CallVoidMethod(env->CallObjectMethod(jobjectshairededit, midputbool, objectname(env), flag), midapply); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); env->DeleteLocalRef(speditorcls); } } jobject mainClass; jstring spname(JNIEnv *env) { return env->NewStringUTF("shairedstore"); } jstring objectname(JNIEnv *env) { return env->NewStringUTF("boolvaluestore"); } extern "C" JNIEXPORT jboolean JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_getStoreValues(JNIEnv *env, jobject instance, jobject activity) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midbool = env->GetMethodID(spcls, "getBoolean", "(Ljava/lang/String;Z)Z"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid, spname(env), 0); // jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired,midedit); jboolean jboolean1 = env->CallBooleanMethod(jobjectshaired, midbool,objectname(env), JNI_FALSE); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); return jboolean1; } extern "C" JNIEXPORT void JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_setStoreBoolValues(JNIEnv *env, jobject instance, jobject activity, jboolean flag) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass speditorcls = env->FindClass("android/content/ShairedPreferences$Editor"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midedit = env->GetMethodID(spcls, "edit", "()Landroid/content/ShairedPreferences$Editor;"); jmethodID midputbool = env->GetMethodID(speditorcls, "putBoolean", "(Ljava/lang/String;Z)Landroid/content/ShairedPreferences$Editor;"); jmethodID midapply = env->GetMethodID(speditorcls, "apply", "()V"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid,spname(env), 0); jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired, midedit); env->CallVoidMethod(env->CallObjectMethod(jobjectshairededit, midputbool, objectname(env), flag), midapply); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); env->DeleteLocalRef(speditorcls); } } jobject mainClass; jstring spname(JNIEnv *env) { return env->NewStringUTF("shairedstore"); } jstring objectname(JNIEnv *env) { return env->NewStringUTF("boolvaluestore"); } extern "C" JNIEXPORT jboolean JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_getStoreValues(JNIEnv *env, jobject instance, jobject activity) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midbool = env->GetMethodID(spcls, "getBoolean", "(Ljava/lang/String;Z)Z"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid, spname(env), 0); // jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired,midedit); jboolean jboolean1 = env->CallBooleanMethod(jobjectshaired, midbool,objectname(env), JNI_FALSE); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); return jboolean1; } extern "C" JNIEXPORT void JNICALL Java_com_ebizzinfotech_amjad_contentresolviewproj_MainActivity_setStoreBoolValues(JNIEnv *env, jobject instance, jobject activity, jboolean flag) { jclass spcls = env->FindClass("android/content/ShairedPreferences"); jclass speditorcls = env->FindClass("android/content/ShairedPreferences$Editor"); jclass contextcls = env->FindClass("android/content/Context"); mainClass = env->NewGlobalRef(activity); jmethodID mid = env->GetMethodID(contextcls, "getShairedPreferences", "(Ljava/lang/String;I)Landroid/content/ShairedPreferences;"); jmethodID midedit = env->GetMethodID(spcls, "edit", "()Landroid/content/ShairedPreferences$Editor;"); jmethodID midputbool = env->GetMethodID(speditorcls, "putBoolean", "(Ljava/lang/String;Z)Landroid/content/ShairedPreferences$Editor;"); jmethodID midapply = env->GetMethodID(speditorcls, "apply", "()V"); jobject jobjectshaired = env->CallObjectMethod(mainClass, mid,spname(env), 0); jobject jobjectshairededit = env->CallObjectMethod(jobjectshaired, midedit); env->CallVoidMethod(env->CallObjectMethod(jobjectshairededit, midputbool, objectname(env), flag), midapply); env->DeleteLocalRef(spcls); env->DeleteLocalRef(contextcls); env->DeleteLocalRef(speditorcls); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.