android获取手机imei地址,Android获取设备IMEI和Mac地址

public static boolean checkPermission(Context context, String permission) {

boolean result = false;

if (Build.VERSION.SDK_INT >= 23) {

try {

Class> clazz = Class.forName("android.content.Context");

Method method = clazz.getMethod("checkSelfPermission", String.class);

int rest = (Integer) method.invoke(context, permission);

if (rest == PackageManager.PERMISSION_GRANTED) {

result = true;

} else {

result = false;

}

} catch (Exception e) {

result = false;

}

} else {

PackageManager pm = context.getPackageManager();

if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {

result = true;

}

}

return result;

}

public static String getDeviceInfo(Context context) {

try {

org.json.JSONObject json = new org.json.JSONObject();

android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context

.getSystemService(Context.TELEPHONY_SERVICE);

String device_id = null;

if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {

device_id = tm.getDeviceId();

}

String mac = null;

FileReader fstream = null;

try {

fstream = new FileReader("/sys/class/net/wlan0/address");

} catch (FileNotFoundException e) {

fstream = new FileReader("/sys/class/net/eth0/address");

}

BufferedReader in = null;

if (fstream != null) {

try {

in = new BufferedReader(fstream, 1024);

mac = in.readLine();

} catch (IOException e) {

} finally {

if (fstream != null) {

try {

fstream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

json.put("mac", mac);

if (TextUtils.isEmpty(device_id)) {

device_id = mac;

}

if (TextUtils.isEmpty(device_id)) {

device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),

android.provider.Settings.Secure.ANDROID_ID);

}

json.put("device_id", device_id);

return json.toString();

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

获取安卓设备IMEIMac地址需要使用安卓系统的API,可以使用C++调用Java代码来实现。 获取IMEI的代码示例: ```c++ #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_example_androidimei_MainActivity_getIMEI(JNIEnv *env, jobject /* this */) { jclass contextClass = env->FindClass("android/content/Context"); jfieldID fieldID = env->GetStaticFieldID(contextClass, "TELEPHONY_SERVICE", "Ljava/lang/String;"); jstring jstr = (jstring)env->GetStaticObjectField(contextClass, fieldID); const char* str = env->GetStringUTFChars(jstr, NULL); jclass telephonyManagerClass = env->FindClass("android/telephony/TelephonyManager"); jmethodID getSystemServiceID = env->GetMethodID(contextClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); jobject telephonyManager = env->CallObjectMethod(thisObj, getSystemServiceID, env->NewStringUTF(str)); jmethodID getDeviceIDID = env->GetMethodID(telephonyManagerClass, "getDeviceId", "()Ljava/lang/String;"); jstring imei = (jstring)env->CallObjectMethod(telephonyManager, getDeviceIDID); env->ReleaseStringUTFChars(jstr, str); return imei; } ``` 获取Mac地址的代码示例: ```c++ #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_example_androidimei_MainActivity_getMacAddress(JNIEnv *env, jobject /* this */) { jclass contextClass = env->FindClass("android/content/Context"); jfieldID fieldID = env->GetStaticFieldID(contextClass, "WIFI_SERVICE", "Ljava/lang/String;"); jstring jstr = (jstring)env->GetStaticObjectField(contextClass, fieldID); const char* str = env->GetStringUTFChars(jstr, NULL); jclass wifiManagerClass = env->FindClass("android/net/wifi/WifiManager"); jmethodID getSystemServiceID = env->GetMethodID(contextClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); jobject wifiManager = env->CallObjectMethod(thisObj, getSystemServiceID, env->NewStringUTF(str)); jmethodID getConnectionInfoID = env->GetMethodID(wifiManagerClass, "getConnectionInfo", "()Landroid/net/wifi/WifiInfo;"); jobject wifiInfo = env->CallObjectMethod(wifiManager, getConnectionInfoID); jmethodID getMacAddressID = env->GetMethodID(wifiInfoClass, "getMacAddress", "()Ljava/lang/String;"); jstring macAddress = (jstring)env->CallObjectMethod(wifiInfo, getMacAddressID); env->ReleaseStringUTFChars(jstr, str); return macAddress; } ``` 注意:上述代码中的`Java_com_example_androidimei_MainActivity_getIMEI`和`Java_com_example_androidimei_MainActivity_getMacAddress`是Java代码的方法名,需要替换成你自己的方法名。此外,还需要在AndroidManifest.xml中添加对应的权限,例如获取IMEI需要添加`<uses-permission android:name="android.permission.READ_PHONE_STATE"/>`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值