当前位置:网站首页>获取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;
}
边栏推荐
猜你喜欢

(一)Multisim安装与入门

No-code development platform application visible permission setting introductory tutorial

(HR Interview) Most Common Interview Questions and Skilled Answers

接口自动化框架,lm-easytest内测版发布,赶紧用起来~

pytorch学习记录(六):循环神经网络 RNN & LSTM

权威推荐!腾讯安全DDoS边缘安全产品获国际研究机构Omdia认可

重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践

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

程序员修炼之道:务以己任,实则明心——通向务实的最高境界

3年软件测试经验面试要求月薪22K,明显感觉他背了很多面试题...
随机推荐
Eleven BUUCTF questions (06)
产品年度营销计划书
UPC2022暑期个人训练赛第19场(B,P)
【Pytorch】如何在关闭batch-norm的同时保持Dropout的开启
CF780G Andryusha and Nervous Barriers
接口自动化框架,lm-easytest内测版发布,赶紧用起来~
SQL 26 calculation under 25 years of age or older and the number of users
HCIP(第十五天) —— 交换机(一)
Synology system installation related file sharing
代码杂谈:从一道面试题看学会Rust的难度
jsArray数组复制方法性能测试2207300823
js背景切换时钟js特效代码
剑指 Offer 05. 替换空格
TaskDispatcher source code parsing
[论文翻译] Unpaired Image-To-Image Translation Using Cycle-Consistent Adversarial Networks
逻辑漏洞----权限类漏洞
业内人士真心话:只会测试没有前途的,我慌了......
永州动力电池实验室建设合理布局方案
数据中台建设(五):打破企业数据孤岛和提取数据价值
js男女身高体重关系图