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;
}

 

人已赞赏
Android文章

Android ScrollView取消惯性滚动

2020-4-15 21:46:49

Android文章

Android 截屏

2020-4-15 23:01:48

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索