I want to get IMEI from c to JNI , I use the code below, but I met an error that cls could not get, it always returned NULL . I checked env and context, they were fine. Why couldn't I get the Context class? I searched from the Internet, someone says that we should use java/lang/Object instead of using android/context/Context , I tried this, it works, cls was no longer NULL, but caused a new error when I call getSystemService . maybe because generic java/lang/Object does not have this method. Can anyone help me? 3Q.
char * get_dev_id(JNIEnv *env, jobject context) { jclass cls = (*env)->FindClass(env, "android/context/Context"); // ---------------- cls == NULL all the time jmethodID mid = (*env)->GetMethodID(env, cls, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); jfieldID fid = (*env)->GetStaticFieldID(env, cls, "TELEPHONY_SERVICE", "Ljava/lang/String;"); jstring str = (*env)->GetStaticObjectField(env, cls, fid); jobject telephony = (*env)->CallObjectMethod(env, context, mid, str); cls = (*env)->FindClass(env, "android/telephony/TelephonyManager"); mid =(*env)->GetMethodID(env, cls, "getDeviceId", "()Ljava/lang/String;"); str = (*env)->CallObjectMethod(env, telephony, mid); jsize len = (*env)->GetStringUTFLength(env, str); char* deviceId = calloc(len + 1, 1); (*env)->GetStringUTFRegion(env, str, 0, len, deviceId); (*env)->DeleteLocalRef(env, str); /* to get a string in deviceId */ return deviceId; }
source share