final TelephonyManager tm = (TelephonyManager) getContext().getSystemService
(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContext()
.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() <<
32) | tmSerial.hashCode());
deviceID = deviceUuid.toString();
直接这样写getDeviceId和getSimSerialNumber报红,原因时没有判断是否授权,添加一个判断即可。
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
final TelephonyManager tm = (TelephonyManager) getContext().getSystemService
(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContext()
.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() <<
32) | tmSerial.hashCode());
deviceID = deviceUuid.toString();
}