How to get Advertising ID in android programmatically

前端 未结 12 1564
迷失自我
迷失自我 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:35

    I might be late but this might help someone else!

        AsyncTask task = new AsyncTask() {
            @Override
            protected String doInBackground(Void... params) {
                AdvertisingIdClient.Info idInfo = null;
                try {
                    idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String advertId = null;
                try{
                    advertId = idInfo.getId();
                }catch (NullPointerException e){
                    e.printStackTrace();
                }
    
                return advertId;
            }
    
            @Override
            protected void onPostExecute(String advertId) {
                Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_SHORT).show();
            }
    
        };
        task.execute();
    

提交回复
热议问题