当前位置:网站首页>获取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;
}
边栏推荐
猜你喜欢
OFDM Sixteen Lectures 3- OFDM Waveforms
No-code development platform all application settings introductory tutorial
(HR Interview) Most Common Interview Questions and Skilled Answers
【Advanced Mathematics】【7】Double Integral
高性能数据访问中间件 OBProxy(三):问题排查和服务运维
jsArray数组复制方法性能测试2207292307
cpu / CS 和 IP
Eleven BUUCTF questions (06)
无代码开发平台应用可见权限设置入门教程
PyQt5快速开发与实战 8.6 设置样式
随机推荐
ARC115F Migration
js人均寿命和GDP散点图统计样式
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
pytorch学习记录(五):卷积神经网络的实现
无代码开发平台应用可见权限设置入门教程
AT4108 [ARC094D] Normalization
SQL 改写系列七:谓词移动
简单理解精确率(Precision),召回率(Recall),准确率(Accuracy),TP,TN,FP,FN
自动化测试的生命周期是什么?
判断链表是否有环
LeetCode二叉树系列——515.最每个树行中找最大值
第十四天笔记
canvas彩虹桥动画js特效
BI-SQL丨WHILE
重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
[Advanced ROS] Lecture 11 Robot co-simulation based on Gazebo and Rviz (motion control and sensors)
jsArray数组复制方法性能测试2207300040
js背景切换时钟js特效代码
Composer安装方式
No-code development platform application visible permission setting introductory tutorial