java.lang.NoSuchFieldError:否“ Ljava / lang / String;”现场JNI

时间:2018-11-19 09:14:53

标签: android c++ android-ndk java-native-interface

美好的一天。请告诉我我在做什么错。实现了从android / provider / Settings $ System获取密钥。诸如ACCELEROMETER_ROTATION之类的键看起来没有问题(描述为https://developer.android.com/reference/android/provider/Settings.System的那些键),但诸如sms_delivered_sound之类的键却拒绝搜索。写入错误。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.woodman.testlibsettingsv2, PID: 31133
    java.lang.NoSuchFieldError: no "Ljava/lang/String;" field "sms_delivered_sound" in class "Landroid/provider/Settings$System;" or its superclasses
        at com.example.woodman.testlibsettingsv2.frmMain.GetSystemKyesString(Native Method)
        at com.example.woodman.testlibsettingsv2.frmMain.onCreate(frmMain.java:32)
        at android.app.Activity.performCreate(Activity.java:7210)
        at android.app.Activity.performCreate(Activity.java:7201)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

代码

    extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_woodman_testlibsettingsv2_frmMain_GetSystemKyesString(JNIEnv *env,
                                                                       jobject instance,
                                                                       jstring key_Name_) {
    const char *key_Name = env->GetStringUTFChars(key_Name_, 0);

    // TODO
    jclass activityThread = env->FindClass("android/app/ActivityThread");
    jmethodID currentActivityThread = env->GetStaticMethodID(activityThread,
                                                             "currentActivityThread",
                                                             "()Landroid/app/ActivityThread;");
    jobject at = env->CallStaticObjectMethod(activityThread, currentActivityThread);

    jmethodID getApplication = env->GetMethodID(activityThread, "getApplication",
                                                "()Landroid/app/Application;");
    jobject context = env->CallObjectMethod(at, getApplication);


    jclass c_settings_system = env->FindClass("android/provider/Settings$System");
    jclass c_context = env->FindClass("android/content/Context");
    if (c_settings_system == NULL || c_context == NULL) {
        return NULL;
    }
    jmethodID m_get_content_resolver = env->GetMethodID(c_context, "getContentResolver",
                                                        "()Landroid/content/ContentResolver;");
    if (m_get_content_resolver == NULL) {
        return NULL;
    }

    jfieldID f_android_string_key = env->GetStaticFieldID(c_settings_system,
                                                          key_Name,
                                                          "Ljava/lang/String;");

    if (f_android_string_key == NULL) {
        return NULL;
    }
    jstring s_android_string_key = (jstring) env->GetStaticObjectField(c_settings_system,
                                                                       f_android_string_key);


    jobject o_content_resolver;
    o_content_resolver = env->CallObjectMethod(context,
                                               m_get_content_resolver);
    if (o_content_resolver == NULL || s_android_string_key == NULL) {
        return NULL;
    }

    jmethodID m_get_string = env->GetStaticMethodID(c_settings_system, "getString",
                                                    "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

    if (m_get_string == NULL) {
        return NULL;
    }

    jstring string_key = (jstring) env->CallStaticObjectMethod(c_settings_system,
                                                                       m_get_string,
                                                                       o_content_resolver,
                                                                       s_android_string_key);
    return string_key;
}

谢谢

1 个答案:

答案 0 :(得分:1)

此值不属于系统设置。实际上,它可以在miui framework上找到,但不能在其他Android系统上找到。

没有公共API可以读取此设置,C ++和Java都没有。但是我们知道此值以简单的key = value格式写入system/build.prop文件中。

每个人都可以读取此文件。您不需要JNI来读取它,通常的C ++甚至C文件API都可以完美地工作。

一种更灵活的方法是从您的代码中运行settings命令。