当前位置:网站首页>获取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;
}
边栏推荐
猜你喜欢
随机推荐
产品年度营销计划书
ARC115F Migration
【软考软件评测师】自动化测试章节下篇
CF603E Pastoral Oddities
Data Middle Office Construction (5): Breaking Enterprise Data Silos and Extracting Data Value
There is a risk of water ingress in the battery pack tray and there is a potential safety hazard. 52,928 Tang DMs are urgently recalled
eclipse连接SQL server数据库「建议收藏」
“12306” 的架构到底有多牛逼
学习笔记——七周成为数据分析师《第一周:数据分析思维》
Composer安装方式
Shell变量与赋值、变量运算、特殊变量、重定向与管渠
No-code development platform application visible permission setting introductory tutorial
AT4108 [ARC094D] Normalization
R语言ggplot2可视化时间序列数据(默认时间中断部分前后自动连接起来)、创建时间分组、使用分面图(faceting)可视化时间序列数据
无代码开发平台全部应用设置入门教程
(论文翻译]未配对Image-To-Image翻译使用Cycle-Consistent敌对的网络
HCIP(第十五天) —— 交换机(一)
激光雷达点云语义分割论文阅读小结
CF338E Optimize!
No-code development platform all application settings introductory tutorial









