How to get Advertising ID in android programmatically

前端 未结 12 1565
迷失自我
迷失自我 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() {
            @Override
            public String call() throws Exception {
                return AdvertisingIdClient.getAdvertisingIdInfo(context).getId();
            }
        })
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Action1() {
                      @Override
                      public void call(String id) {
                         //do what you want to do with id for e.g using it for tracking
                      }
                  }, new Action1() {
                      @Override
                      public void call(Throwable throwable) {
                          throwable.printStackTrace();
                      }
                  });
    }
    

提交回复
热议问题