当前位置:网站首页>获取Google Advertising ID作为唯一识别码
获取Google Advertising ID作为唯一识别码
2022-07-30 13:45:00 【互联网小熊猫】
一、背景
在Android程序中,有时候我们APP需要获取唯一识别码,来区别用户。在Android系统中提供了了AndroidId,但AndroidId有时候会为null,同时root手机用户,androidid可以改变。所以AndroidId并不能作为唯一识别码。
对于Google推荐使用Google Advertising ID,通过Google Service可以获取Google Advertising ID(如果没有Google Service就回去不到Google Advertising ID)。
二、代码实现
/** * 这个方法是耗时的,不能在主线程调用 */
public static String getGoogleAdId(Context context) throws Exception {
if (Looper.getMainLooper() == Looper.myLooper()) {
return "Cannot call in the main thread, You must call in the other thread";
}
AdvertisingIdClient.Info idInfo = null;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(ctx);
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
String adid = null;
try {
if (idInfo != null) {
adid = idInfo.getId();
}
} catch (NullPointerException e) {
e.printStackTrace();
}
return adid;
}
边栏推荐
- #第九章 子查询课后习题
- [Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
- Shell变量与赋值、变量运算、特殊变量、重定向与管渠
- 20220729 证券、金融
- 地形分析的主要内容(流浪地球的特效水平)
- js背景切换时钟js特效代码
- SQL 26 calculation under 25 years of age or older and the number of users
- 一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
- TaskDispatcher source code parsing
- 激光雷达点云语义分割论文阅读小结
猜你喜欢

重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践

VLAN实验

无代码开发平台应用可见权限设置入门教程

LeetCode二叉树系列——145.二叉树的后序遍历

近两年激光雷达运动物体分割论文阅读小结

There is a risk of water ingress in the battery pack tray and there is a potential safety hazard. 52,928 Tang DMs are urgently recalled

svg波浪动画js特效代码

The path to uniting the programmer: "titles bucket" to the highest state of pragmatic

还在说软件测试没有中年危机?9年测试工程师惨遭淘汰

权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可
随机推荐
CF1320E Treeland and Viruses
CF603E Pastoral Oddities
What is the level of Ali P7?
打破原则引入SQL,MongoDB到底想要干啥???
为什么做软件测试一定要学自动化?谈谈我眼中自动化测试的价值
LeetCode二叉树系列——144.二叉树的最大深度
剑指 Offer 05. 替换空格
odoo--qweb模板介绍(一)
二手手机销量突破3亿部,与降价的iPhone夹击国产手机
CF603E Pastoral Oddities
05 | login background: based on the password login mode (below)
kubernate部署服务
strlen跟sizeof区别
How awesome is the "12306" architecture?
接口自动化框架,lm-easytest内测版发布,赶紧用起来~
第十五天笔记
LeetCode二叉树系列——144.二叉树的最小深度
HCIP(第十五天) —— 交换机(一)
pytorch学习记录(五):卷积神经网络的实现
创意loadingjs特效小点跳跃动画