当前位置:网站首页>获取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;
}
边栏推荐
- 为什么做软件测试一定要学自动化?谈谈我眼中自动化测试的价值
- SQL 26 calculation under 25 years of age or older and the number of users
- #第九章 子查询课后习题
- 戴墨镜的卡通太阳SVG动画js特效
- 手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
- R语言使用aov函数进行单因素协方差分析(One-way ANCOVA)、使用effects包中的effect函数来计算调整后的分组均值(calculate adjusted means)
- VLAN实验
- 永州动力电池实验室建设合理布局方案
- 05 | login background: based on the password login mode (below)
- CF338E Optimize!
猜你喜欢
随机推荐
R语言使用aov函数进行单因素协方差分析(One-way ANCOVA)、使用effects包中的effect函数来计算调整后的分组均值(calculate adjusted means)
CF1320E Treeland and Viruses
接口自动化框架,lm-easytest内测版发布,赶紧用起来~
selenium4+pyetsst+allure+pom进行自动化测试框架的最新设计
43.【list的简单属性】
权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可
自动化测试的生命周期是什么?
LeetCode二叉树系列——145.二叉树的后序遍历
手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
判断链表是否有环
cpu/CS and IP
打破原则引入SQL,MongoDB到底想要干啥???
六面蚂蚁金服,抗住面试官的狂轰乱炸,前来面试复盘
无代码开发平台应用可见权限设置入门教程
记面试外包公司的一次经历,到底该不该去?
[C# 循环跳转]-C# 中的 while/do-while/for/foreach 循环结构以及 break/continue 跳转语句
redis6.0 源码学习(五)ziplist
pytorch学习记录(五):卷积神经网络的实现
以unity3d为例解读:游戏数据加密
shell脚本流程控制语句









