nima moradi
nima moradi

Reputation: 2628

can not get Advertising ID Provider in android

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

Upvotes: 8

Views: 10277

Answers (5)

Aneh Thakur
Aneh Thakur

Reputation: 1100

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?

Upvotes: 0

Farhad
Farhad

Reputation: 189

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'

Upvotes: 1

Akshay Kumar Both
Akshay Kumar Both

Reputation: 382

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'
}

Upvotes: 0

Moorthy
Moorthy

Reputation: 301

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'
}

Upvotes: 2

Dmytro Batyuk
Dmytro Batyuk

Reputation: 984

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

Upvotes: 0

Related Questions