How to get Advertising ID in android programmatically

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

    Just in case someone is interested in trying out the fetching AdvertisingId part while Rx-ing then this might be helpful.

    private void fetchAndDoSomethingWithAdId() {
        Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return AdvertisingIdClient.getAdvertisingIdInfo(context).getId();
            }
        })
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Action1<String>() {
                      @Override
                      public void call(String id) {
                         //do what you want to do with id for e.g using it for tracking
                      }
                  }, new Action1<Throwable>() {
                      @Override
                      public void call(Throwable throwable) {
                          throwable.printStackTrace();
                      }
                  });
    }
    
    0 讨论(0)
  • 2020-12-08 04:48

    Fetch the advertising id from background thread:

        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
                    String adId = adInfo != null ? adInfo.getId() : null;
                    // Use the advertising id
                } catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException exception) {
                    // Error handling if needed
                }
            }
        });
    

    I added null checks to prevent any crashes. The Google example implementation code would crash with a NullPointerException if an exception occures.

    0 讨论(0)
  • 2020-12-08 04:50

    You only need this package: implementation("com.google.android.gms:play-services-ads-identifier:17.0.0") It's not really listed anywhere but it's published on Mavne.

    Get Google Services using GoogleApiAvailabilityLight.getInstance

    0 讨论(0)
  • 2020-12-08 04:51

    With OS validation.

    Call this in an AsyncTask

    /** Retrieve the Android Advertising Id 
         * 
         * The device must be KitKat (4.4)+ 
         * This method must be invoked from a background thread.
         * 
         * */
        public static synchronized String getAdId (Context context) {
    
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
                return null;
            }
    
            AdvertisingIdClient.Info idInfo = null;
            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
            } 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;
        }
    
    0 讨论(0)
  • 2020-12-08 04:52
    import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
    
    
    Info adInfo = null;
    
    try {
         adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
    } catch (IOException e) {
         e.printStackTrace();
    } catch (GooglePlayServicesAvailabilityException e) {
         e.printStackTrace();
    } catch (GooglePlayServicesNotAvailableException e) {
         e.printStackTrace();
    }
    
    String AdId = adInfo.getId();
    

    You need add gms libs otherwise you cannot get the advertising id. It can be reset by user or when you do a factory reset (at factory reset time the Android id also reset).

    0 讨论(0)
  • 2020-12-08 04:57

    You can call the below function in onCreate(Bundle savedInstanceState) of the activity

    and in the logcat search for UIDMY then it will display the id like : I/UIDMY: a1cf5t4e-9eb2-4342-b9dc-10cx1ad1abe1

    void getUIDs()
    {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(SplashScreen.this);
                    String myId = adInfo != null ? adInfo.getId() : null;
    
                   Log.i("UIDMY",myId);
                } catch (Exception e) {
                     Toast toast = Toast.makeText(conext, "error occured ", Toast.LENGTH_SHORT);
        toast.setGravity(gravity, 0,0);
        toast.show();
    
                }
            }
        });
    }
    
    0 讨论(0)
提交回复
热议问题