AdvertisingIdClient getAdvertisingIdInfo hangs forever

前端 未结 2 1407
清歌不尽
清歌不尽 2021-02-20 03:54

I\'m trying to get advertising ID from Google Play services API. Here is a sample code:

...
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
imp         


        
2条回答
  •  离开以前
    2021-02-20 04:02

    Unfortunately the getAdvertisingIdInfo call needs to be done from a background thread, you should not block the main thread while invoking it. Seems like there is no option to get the AdId synchronously.

    ---- EDIT----

    I could achieve this by running on a new thread. Here is what finally work for me. It is not hanging anymore (May not be an ideal)

    getGAIDSync(){
     final CountDownLatch  latch  = new CountDownLatch(1);
     new Thread(new Runnable() {
                @Override
                public void run() {               
                    getGoogleAdsIDAsyncTask.execute().get(5000, TimeUnit.MilliSecond);
                   latch.countDown();                
        }}).start();
        try {
            latch.await(5000,TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
             e.printStackTrace();
        }
    
    }
    

提交回复
热议问题