Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get device id with JNI in Android?
    primarykey
    data
    text
    <p>I want to get IMEI from c by <code>JNI</code>, I use the code below, but met the error that failed to get <code>cls</code>, it always returned <code>NULL</code>.I checked env and context, they were all right.Why couldn't me get the <code>Context</code> class? I searched from the internet, someone says we should use <code>java/lang/Object</code> instead of using <code>android/context/Context</code>, I tried this, it works, cls wasn't NULL any longer,but it caused new error when I call <code>getSystemService</code>, maybe because common <code>java/lang/Object</code> dosn't have this method.Could anyone help me?3Q. </p> <pre><code>char * get_dev_id(JNIEnv *env, jobject context) { jclass cls = (*env)-&gt;FindClass(env, "android/context/Context"); // ---------------- cls == NULL all the time jmethodID mid = (*env)-&gt;GetMethodID(env, cls, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); jfieldID fid = (*env)-&gt;GetStaticFieldID(env, cls, "TELEPHONY_SERVICE", "Ljava/lang/String;"); jstring str = (*env)-&gt;GetStaticObjectField(env, cls, fid); jobject telephony = (*env)-&gt;CallObjectMethod(env, context, mid, str); cls = (*env)-&gt;FindClass(env, "android/telephony/TelephonyManager"); mid =(*env)-&gt;GetMethodID(env, cls, "getDeviceId", "()Ljava/lang/String;"); str = (*env)-&gt;CallObjectMethod(env, telephony, mid); jsize len = (*env)-&gt;GetStringUTFLength(env, str); char* deviceId = calloc(len + 1, 1); (*env)-&gt;GetStringUTFRegion(env, str, 0, len, deviceId); (*env)-&gt;DeleteLocalRef(env, str); /* to get a string in deviceId */ return deviceId; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload