Android Q適配之IMEI替換為Android_id
前置工作:
項目配置升到對應(yīng)的29版本
compileSdkVersion: 29,
buildToolsVersion: ‘29.0.0',
minSdkVersion : 19,
targetSdkVersion : 29,
javaVersion : JavaVersion.VERSION_1_8

升級到Android Q后的權(quán)限提示界面
老版本獲取IMEI的方法:
public static String getIMEI(Context context) {
String deviceId = null;
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
deviceId = tm.getDeviceId();
if (deviceId == null || "".equals(deviceId)) {
return getLocalMacAddress(context);
}
} catch (Exception e) {
e.printStackTrace();
if (deviceId == null || "".equals(deviceId)) {
return getLocalMacAddress(context);//獲取Mac地址,在Android 9 P版本中,地址會隨機變化,不可用作唯一標(biāo)識,可去掉。
}
}
return deviceId;
}
Android Q獲取IMEI方法
public static String getIMEI(Context context) {
String deviceId = null;
try {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
deviceId = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
} else {
// request old storage permission
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return null;
}
deviceId = tm.getDeviceId();
}
if (deviceId == null || "".equals(deviceId)) {
return getLocalMacAddress(context);
}
} catch (Exception e) {
e.printStackTrace();
if (deviceId == null || "".equals(deviceId)) {
return getLocalMacAddress(context);
}
}
return deviceId;
}
谷歌官方有聲明:手機恢復(fù)出廠設(shè)置,Android ID會重置。
如果用戶拒絕權(quán)限,也還是會獲取不到設(shè)備標(biāo)識。
所以具體優(yōu)化需自行結(jié)合開發(fā)情景,有更好的建議的道友可以評論補充說明^ - ^…
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中DownloadManager實現(xiàn)文件下載實例詳解
這篇文章主要介紹了Android中DownloadManager實現(xiàn)文件下載實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android中使用ZXing生成二維碼(支持添加Logo圖案)
ZXing是谷歌的一個開源庫,可以用來生成二維碼、掃描二維碼。接下來通過本文給大家介紹Android中使用ZXing生成二維碼(支持添加Logo圖案),需要的朋友參考下2017-01-01
Android 藍(lán)牙自動匹配PIN碼跳過用戶交互示例
本篇文章主要介紹了Android 藍(lán)牙自動匹配PIN碼跳過用戶交互示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06

