gpt4 book ai didi

java - Android Studio 使用官方代码 fragment 获取广告 ID

转载 作者:行者123 更新时间:2023-12-01 17:44:13 26 4
gpt4 key购买 nike

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、执行器

Supposedly it asks for executor but the official snippet is without one

最佳答案

如果你查看文档 here ,kotlin 代码 fragment 传递一个 Executor 作为第三个参数(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());

关于java - Android Studio 使用官方代码 fragment 获取广告 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60891008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com