Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 832
nima moradi Avatar asked Aug 17 '19 07:08

nima moradi


People also ask

How do I find my Android advertising ID?

Android – Find your Advertising ID Simply open the Google Settings app on your Android device and click on “Ads.” Your Advertising Identifier will be listed at the bottom of the screen.

How do I get a new advertising ID?

To reset your Android advertising ID, Open Google Settings on your Android device by tapping on menu and then on Google Settings once all apps are displayed on the screen. Locate and tap on the Ads menu under Services. Tap on “reset advertising ID” on the new page.

What is advertising ID permission in Android?

The advertising ID is a unique, user-resettable ID for advertising, provided by Google Play services. It gives users better controls and provides developers with a simple, standard system to continue to monetize their apps.

Do Android phones have IDFA?

The ad identifier - aka “IDFA” on iOS, or “AAID” on Android - is the key that enables most third-party tracking on mobile devices. Disabling it will make it substantially harder for advertisers and data brokers to track and profile you, and will limit the amount of your personal information up for sale.


2 Answers

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'
}
like image 197
Moorthy Avatar answered Oct 11 '22 02:10

Moorthy


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'

like image 30
Farhad Avatar answered Oct 11 '22 03:10

Farhad