如何在调用JNI方法时获取Android上下文实例?

时间:2017-10-22 02:21:26

标签: java android java-native-interface native-code

我已经在stackoverflow上阅读了一些相关的答案,但似乎没有人回答我的问题。我将从本机代码中获取android ID,即。在C代码中调用方法getAndroidIDfromNativeCode(因此JVM通过方法create_vm在本机代码中引入),您知道在调用方法 getContentResolver 时,您必须使用 Android Context实例< / strong>要调用它,那么如何获取上下文实例

    static jstring
    native_code_getAndroidID(JNIEnv *env, jobject thiz)
    {
        jclass c_settings_secure = (*env)->FindClass(env, "android/provider/Settings$Secure");
        jclass c_context = (*env)->FindClass(env,"android/content/Context");
        if(c_settings_secure == NULL || c_context == NULL){
            return NULL;
        }
        //Get the getContentResolver method
        jmethodID m_get_content_resolver = (*env)->GetMethodID(env, c_context, "getContentResolver",
                                                               "()Landroid/content/ContentResolver;");
        if(m_get_content_resolver == NULL){
            return NULL;
        }
        //Get the Settings.Secure.ANDROID_ID constant
        jfieldID f_android_id = (*env)->GetStaticFieldID(env, c_settings_secure, "ANDROID_ID", "Ljava/lang/String;");

        if(f_android_id == NULL){
            return NULL;
        }
        jstring s_android_id = (*env)->GetStaticObjectField(env, c_settings_secure, f_android_id);

        //create a ContentResolver instance context.getContentResolver()
        /*
          where can I get the context instance from Anroid APP??
        */
        jobject o_content_resolver = (*env)->CallObjectMethod(env, context, m_get_content_resolver);
        if(o_content_resolver == NULL || s_android_id == NULL){
            return NULL;
        }
        //get the method getString
        jmethodID m_get_string = (*env)->GetStaticMethodID(env, c_settings_secure, "getString",
                                                           "(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

        if(m_get_string == NULL){
            return NULL;
        }
        //get the Android ID
        jstring android_id = (*env)->CallStaticObjectMethod(env, c_settings_secure,
                                                            m_get_string,
                                                            o_content_resolver,
                                                            s_android_id);
        return android_id;
    }

    JNIEnv* create_vm() {
      JavaVM* jvm;
      JNIEnv* env;
      JavaVMInitArgs args;
      JavaVMOption options[1];

      /* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
      args.version = JNI_VERSION_1_4;
      args.nOptions = 1;
      options[0].optionString = "-Djava.class.path=-jar-path";
      args.options = options;
      args.ignoreUnrecognized = JNI_FALSE;

      JNI_CreateJavaVM(&jvm, (void **)&env, &args);
      return env;
    }

    char *jstringTostr(jstring android)
    {
      ...
    }

    //I will call this function from native code.

    char *getAndroidIDfromNativeCode()
    {

      JNIEnv* env = NULL;

      env = create_vm();
      jstring androidID = native_code_getAndroidID(env,NULL);
      return jstringTostr(androidID);

    }

2 个答案:

答案 0 :(得分:0)

类型jobject的最后一个参数是保存Native方法声明的类的实例。

因此,如果您在活动中声明方法,则可以直接使用最后一个参数thiz作为上下文的实例。

否则,您需要在原始声明中添加另一个类型为Context的参数,并在方法定义中添加jobject类型的参数。

答案 1 :(得分:0)

以下方法可以获得上下文实例

@Name