我正在尝试在 android java 中获取 google ad id (GAID),但我收到如下异常:
androidx.ads.identifier.AdvertisingIdNotAvailableException:没有可用的兼容 AndroidX 广告 ID 提供程序。
我的代码
ListenableFuture<AdvertisingIdInfo> adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
System.out.println("adinfo"+adInfo.toString());
try {
String id = adInfo.get().getId();
System.out.println("adod"+id);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我添加了依赖
implementation 'com.google.android.gms:play-services-ads:19.4.0'
implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
Run Code Online (Sandbox Code Playgroud)
在清单中我添加了
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true"/>
Run Code Online (Sandbox Code Playgroud)
我尝试了很多解决方案,但没有用。请帮我解决这个问题。
androidx 中的 gaid 依赖性似乎有问题。
许多人(包括我)都遵循了Google 的广告 ID 文档并报告了相同的问题。此外,谷歌关于此事的文档似乎已经过时,因为示例代码片段是错误的(例如函数addCallback缺少参数)。
解决方案是在您的 gradle 文件中添加此依赖项,而不是您提到的依赖项:
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
然后您可以通过以下方式获取广告 ID:
AdvertisingIdClient.Info idInfo;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
String advertisingId = idInfo.getId();
//do sth with the id
} catch (IOException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如果您只想在用户未选择退出个性化广告时使用 ID,则可以在检索 ID 信息后添加以下内容:
//if user hasn't opted out of ads in device settings
if(idInfo.isLimitAdTrackingEnabled()) {
//do sth with the id
}
Run Code Online (Sandbox Code Playgroud)
编辑:提到的谷歌文档实际上有一个注释,鼓励您使用我提到的广告标识符库:
| 归档时间: |
|
| 查看次数: |
3430 次 |
| 最近记录: |