can not get Advertising ID Provider in android
Asked Answered
S

5

8

i want to get Advertising ID for my app, which i have no success.

import androidx.ads.identifier.AdvertisingIdClient;
import androidx.ads.identifier.AdvertisingIdInfo;

public class addUtilsJava extends AsyncTask<Context, String, String> {

    static String TAG = "addUtilsJava";


    private String getIdThread(Context context) {

        AdvertisingIdInfo adInfo = null;
        try {
            adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context).get();

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        if (adInfo != null) {
            final boolean isLAT = adInfo.isLimitAdTrackingEnabled();
            return adInfo.getId();
        }
        return null;
    }
    @Override
    protected String doInBackground(Context... contexts) {
        return getIdThread(contexts[0]);
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (s != null)
            Log.d(TAG, s);
    }
}

it throw exception that suggesting me androidx.ads.identifier.AdvertisingIdNotAvailableException: No Advertising ID Provider available. i tried with 2 mobile phone and emulator all with same result.

thanks in advance

ps: i check the solution provided in android doc but it won't work for me https://developer.android.com/training/articles/ad-id
and i rather have a solution that don't require dependency

Sarrusophone answered 17/8, 2019 at 7:27 Comment(6)
did you try without androidx using the support lib? Also does your phone has the Play Services?Pentameter
i did not try with old gms-services and all my phone have play servicesSarrusophone
Do you have access to the play store on those phones?Pentameter
i check that with normal phone that have play service enable and i can see ads-id in settingSarrusophone
I'm sorry but was those phones the same that those having the issue? Definitevely you need to assert that it's not related to the androidx lib, and also that using a blank new project you don't have the issue. If you have the issue in a new standalone app, you can share this app to the question.Pentameter
I also have the same issue. I'm able to get ad ID using com.google.android.gms:play-services-ads-identifier lib, but I'm getting AdvertisingIdNotAvailableException when trying to use androidx lib. Obviously, the issue is in androidx lib itself.Cyperaceous
I
2

Try this Code..for me working..

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
                    @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_LONG).show();
                    }

                };
                task.execute();

}

}

build.gradle added dependencies

dependencies {
        implementation 'com.google.android.gms:play-services-ads-lite:18.1.1'
}
Inadvertent answered 21/8, 2019 at 12:9 Comment(1)
Question is related to AndroidX. Answer suggests using Google Play Services artifact. Although solution might work - this is not an answer to the original question.Cyperaceous
F
1

add the following lib into your gradle and get the AdId from there instead. It works perfectly for me

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

Flashboard answered 28/9, 2020 at 14:4 Comment(0)
N
0

Try to use it with callbacks as it's described in training sample. Make all calls from main thread. Hope it would help

Nymphet answered 21/8, 2019 at 11:39 Comment(1)
i test that not working and i don't want to add dependency just to use it onceSarrusophone
K
0

Below is a working solution in Kotlin.

First of all, Add this rule to your app module level build.gradle file in dependency:
implementation 'com.google.android.gms:play-services-ads-lite:11.8.0'

This is AddUtilsJava class:

package com.example.myapplication


import android.content.Context
import android.os.AsyncTask
import android.util.Log
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.google.android.gms.common.GooglePlayServicesNotAvailableException
import com.google.android.gms.common.GooglePlayServicesRepairableException
import java.io.IOException

class AddUtilsJava : AsyncTask<Context, String, String>() {

    private fun getIdThread(context: Context): String? {
        var idInfo: AdvertisingIdClient.Info? = null
        try {
            idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context.applicationContext)
        } catch (e: GooglePlayServicesNotAvailableException) {
            e.printStackTrace()
        } catch (e: GooglePlayServicesRepairableException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }

        var advertId: String? = null
        try {
            advertId = idInfo!!.id
        } catch (e: NullPointerException) {
            e.printStackTrace()
        }

        return advertId
    }

    override fun doInBackground(vararg contexts: Context): String? {
        return getIdThread(contexts[0])
    }

    override fun onPostExecute(s: String?) {
        super.onPostExecute(s)
        if (s != null)
            Log.d(TAG, s)
    }

    companion object {
        internal var TAG = "addUtilsJava"
    }
}

This is build.gradle file:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'

    implementation 'com.google.android.gms:play-services-ads-lite:11.8.0' //for ads

    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
    testImplementation 'junit:junit:4.13-beta-3'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
}
Karrikarrie answered 24/8, 2019 at 22:52 Comment(1)
Question is related to AndroidX. Answer suggests using Google Play Services artifact. Although solution might work - this is not an answer to the original question.Cyperaceous
R
0

You use bellow method to get AAID of your device. Call bellow method inside onCreate method

public void getAAID()
{
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            try {
                AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MyActivity.this);
                String myId = adInfo != null ? adInfo.getId() : null;

               Log.i("UIDMY",myId);
            } catch (Exception e) {
                 Log.e("error", e);
            }
        }
    });
}

Complete information:- How to Get AAID for Facebook Ads?

Rita answered 19/10, 2020 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.