当前位置:网站首页>获取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;
}
边栏推荐
猜你喜欢
记面试外包公司的一次经历,到底该不该去?
LeetCode二叉树系列——145.二叉树的后序遍历
TaskDispatcher源码解析
SQL 26 calculation under 25 years of age or older and the number of users
(HR Interview) Most Common Interview Questions and Skilled Answers
ENVI Image Processing (6): NDVI and Vegetation Index
激光雷达点云语义分割论文阅读小结
shell脚本流程控制语句
How awesome is the "12306" architecture?
Eleven BUUCTF questions (06)
随机推荐
redis6.0 源码学习(五)ziplist
LeetCode二叉树系列——107.二叉树的层序遍历II
Data Middle Office Construction (5): Breaking Enterprise Data Silos and Extracting Data Value
以unity3d为例解读:游戏数据加密
ccs软件的使用(靠谱挣钱的app软件)
学习笔记——七周成为数据分析师《第二周:业务》:业务分析指标
业内人士真心话:只会测试没有前途的,我慌了......
ML之PDP:基于FIFA 2018 Statistics(2018年俄罗斯世界杯足球赛)球队比赛之星分类预测数据集利用DT决策树&RF随机森林+PDP部分依赖图可视化实现模型可解释性之详细攻略
电池包托盘有进水风险,存在安全隐患,紧急召回52928辆唐DM
百家号取消接口发文功能:插外链获权重被堵死
CF338E Optimize!
程序员修炼之道:务以己任,实则明心——通向务实的最高境界
高性能数据访问中间件 OBProxy(三):问题排查和服务运维
Mac Brew 安装PHP
cpu / CS 和 IP
UPC2022暑期个人训练赛第19场(B,P)
jsArray array copy method performance test 2207300823
BUUCTF刷题十一道(06)
无代码开发平台应用可见权限设置入门教程
永州动力电池实验室建设合理布局方案