- 论坛徽章:
- 0
|
本帖最后由 rover12421 于 2013-09-23 17:32 编辑
我想在Native code中获取IMEI,但不想先在Java中实现,再JNI调用,而是想直接在Native中调用getDeviceId去获取,但是在执行getApplicationContext的时候无法成功,有人帮忙看看吗?非常感谢!
- char * get_imei2(JNIEnv *env, jobject context)
- {
- jclass thiz_class = (*env)->FindClass(env, "android/app/Activity");
- jmethodID mid_cxt = (*env)->GetMethodID(env, thiz_class, "getApplicationContext", "()Ljava/lang/Object;");
- jobject app_ctx = (*env)->CallObjectMethod(env, context, mid_cxt); // --------------在这里出错无法通过
- jclass cls = (*env)->FindClass(env, "android/context/Context");
- 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, app_ctx, 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;
- }
复制代码 |
|