簡體   English   中英

Android Studio 使用官方代碼段獲取廣告 ID

[英]Android Studio Get Advertisement Id with the official snippet

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 代碼段將傳遞一個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());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM