How to get Advertising ID in android programmatically

前端 未结 12 1559
迷失自我
迷失自我 2020-12-08 04:18

I want to get users Advertising ID programmatically.I used the below code from the developer site.But its not working

         Info adInfo = null;
                   


        
12条回答
  •  粉色の甜心
    2020-12-08 04:33

    Make sure you have added play identity services, then you can get advertising id by running a thread like this:

    Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
                    String advertisingId = adInfo != null ? adInfo.getId() : null;
                } catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException exception) {
                    exception.printStackTrace();
                }
            }
        };
    
        // call thread start for background process
        thread.start();
    

提交回复
热议问题