当前位置:   article > 正文

android 获取设备的mac地址,Android获取设备IMEI和Mac地址

android app收集imac地址和收集ime

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;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/242252
推荐阅读
相关标签
  

闽ICP备14008679号