简体   繁体   中英

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) {

                        }
            });

''' The error is: error: method addCallback in class Futures cannot be applied to given types;
required: ListenableFuture,FutureCallback,Executor

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

If you check the documentation here , the kotlin snippet passes an Executor as the third parameter (the java sample misses one). So you should do the same in 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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM