Created
October 17, 2017 07:15
-
-
Save zyc945/c1d9b3affdfdbe2aa98455bb502cfee5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment