java - Android Studio 使用官方代码片段获取广告 ID
问题描述
has anyone managed to get the android advertisement Id in an app using the official snippet
https://developer.android.com/training/articles/ad-id
I couldn't make this snippet to work in my app.
I have added the:
'''
dependencies {
implementation 'androidx.ads:ads-identifier:1.0.0-alpha01'
// Used for the calls to addCallback() in the snippets on this page.
implementation 'com.google.guava:guava:28.0-android'
}
'''
And Gradle synced without any problems.
The code that's problematic is:
'''java
ListenableFuture<AdvertisingIdInfo> advertisingIdInfoListenableFuture = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
Futures.addCallback(advertisingIdInfoListenableFuture,
new FutureCallback<AdvertisingIdInfo>() {
@Override
public void onSuccess(@NullableDecl AdvertisingIdInfo result) {
String myAdvertisementId = result.getId();
}
@Override
public void onFailure(Throwable t) {
}
});
''' 错误是:错误:Futures 类中的方法 addCallback 不能应用于给定类型;
必需:ListenableFuture,FutureCallback,Executor
Supposedly it asks for executor but the official snippet is without one
解决方案
如果您在此处查看文档,kotlin 代码片段将 anExecutor
作为第三个参数传递(java 示例缺少一个)。所以你应该在java中做同样的事情:
Futures.addCallback(advertisingIdInfoListenableFuture,
new FutureCallback<AdvertisingIdInfo>() {
@Override
public void onSuccess(@NullableDecl AdvertisingIdInfo result) {
String myAdvertisementId = result.getId();
}
@Override
public void onFailure(Throwable t) {
}
}, Executors.newSingleThreadExecutor());
推荐阅读
- python - 如何编写具有固定输入行的 Python 终端应用程序?
- mysql - Spring CriteriaBuilder 查询 JSONArray 数据
- shell - Shell:如果超时,如何停止命令行?
- python - ttk 样式不适用于 python 2.7.18
- java - Spring Boot 应用程序未启动可能是由于依赖关系
- wpf - 通过 Wix 设置项目将应用程序添加到“选择默认浏览器”
- javascript - 如何在 Code.org App Lab 中显示表格中的值?
- scala - 在 Spark 中,无法使用来自 Kafka 主题的数据
- android - ScrollView 在 ConstraintLayout 和 viewPage 中不起作用
- php - Visual Studio Code 返回 intelephense 1008 错误消息