当前位置:网站首页>获取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;
}
边栏推荐
猜你喜欢
随机推荐
pytorch学习记录(六):循环神经网络 RNN & LSTM
LeetCode二叉树系列——199二叉树的右视图
记面试外包公司的一次经历,到底该不该去?
LeetCode二叉树系列——144.二叉树的最大深度
还在说软件测试没有中年危机?9年测试工程师惨遭淘汰
jsArray array copy method performance test 2207292307
业内人士真心话:只会测试没有前途的,我慌了......
[C# 循环跳转]-C# 中的 while/do-while/for/foreach 循环结构以及 break/continue 跳转语句
[ARC092D] Two Faced Edges
el-table中el-table-column下的操作切换class样式
如何判断自己是否适合IT行业?方法很简单
Data Middle Office Construction (5): Breaking Enterprise Data Silos and Extracting Data Value
SyntaxError: EOL while scanning string literal
(HR面试)最常见的面试问题和技巧性答复
“12306” 的架构到底有多牛逼
永州动力电池实验室建设合理布局方案
什么是缺陷分析?一篇文章带你了解,测试工程师必备技能
学习笔记——七周成为数据分析师《第二周:业务》:业务分析指标
ARC117E Zero-Sum Ranges 2
无代码开发平台应用可见权限设置入门教程









