NDK 获取android的imei和serial number
1,获取imei
int setAndroidDeviceID(JNIEnv *env, jobject obj,jobject mContext) { if(mContext == 0){ return -1; } jclass cls_context = (*env)->FindClass(env, "android/content/Context"); if(cls_context == 0){ return -1; } jmethodID getSystemService = (*env)->GetMethodID(env, cls_context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); if(getSystemService == 0){ return -1; } jfieldID TELEPHONY_SERVICE = (*env)->GetStaticFieldID(env, cls_context, "TELEPHONY_SERVICE", "Ljava/lang/String;"); if(TELEPHONY_SERVICE == 0){ return -1; } jstring str = (*env)->GetStaticObjectField(env, cls_context, TELEPHONY_SERVICE); jobject telephonymanager = (*env)->CallObjectMethod(env, mContext, getSystemService, str); if(telephonymanager == 0){ return -1; } jclass cls_tm = (*env)->FindClass(env, "android/telephony/TelephonyManager"); if(cls_tm == 0){ return -1; } jmethodID getDeviceId = (*env)->GetMethodID(env, cls_tm, "getDeviceId", "()Ljava/lang/String;"); if(getDeviceId == 0){ return -1; } jstring deviceid = (*env)->CallObjectMethod(env, telephonymanager, getDeviceId); DP_IMIE = (*env)->GetStringUTFChars(env, deviceid, 0); return 0; }
2,获取serial number
#include <sys/system_properties.h> void getAndroidDeviceID_Serial(char *deviceID)//serial number { __system_property_get("ro.serialno",deviceID); }