How to get Advertising ID in android programmatically

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

    Using Kotlin & RxJava Observers

    Import in your Gradle file

    implementation 'com.google.android.gms:play-services-ads:15.0.0'
    

    Import on top of your kotlin source file

    import io.reactivex.Observable
    import com.google.android.gms.ads.identifier.AdvertisingIdClient
    

    Implement a helper function

        private fun fetchAdIdAndThen(onNext : Consumer, onError : Consumer) {
            Observable.fromCallable(Callable {
                AdvertisingIdClient.getAdvertisingIdInfo(context).getId()
            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(onNext, onError);
        }
    

    Then

        fetchAdIdAndThen(Consumer() {
            adId ->
            performMyTaskWithADID(activity, 10000, adId);
        }, Consumer() {
            throwable ->
            throwable.printStackTrace();
            performMyTaskWithADID(activity, 10000, "NoADID");
        })
    

提交回复
热议问题