在 Android 13 中,广告 ID 的访问权限将限制为使用适当权限的应用程序。要检查应用程序是否已被授权访问广告 ID,请使用以下代码示例:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// Do not have permission to access advertising ID
} else {
// Have permission to access advertising ID, continue with code
}
此外,API 33 还引入了新的方法来获取广告 ID。以下是示例代码:
AdvertisingIdClient.Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
String advertisingId = adInfo.getId();
boolean limitAdTracking = adInfo.isLimitAdTrackingEnabled();
} catch (IOException e) {
// Error handling code
} catch (GooglePlayServicesNotAvailableException e) {
// Error handling code
} catch (GooglePlayServicesRepairableException e) {
// Error handling code
}
请注意,在使用此新方法时,应捕获可能的异常并进行错误处理。
上一篇:Android12:SDK(31)无法通过意图启动活动
下一篇:Android13(SDK33):Bundle.getSerializable(String)isdeprecated,whatisalternative?