【问题标题】:Android Studio Get Advertisement Id with the official snippetAndroid Studio 使用官方代码片段获取广告 ID
【发布时间】:2020-07-08 11:42:29
【问题描述】:
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

【问题讨论】:

  • 请在此处分享您迄今为止尝试过的代码。
  • 我已经添加了代码。谢谢

标签: java android android-studio code-snippets


【解决方案1】:

如果您查看文档here,kotlin sn-p 将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());

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 2017-08-22
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多