user6615010
user6615010

Reputation: 122

How to get Advertising id in android?

I used this code it's working when i called from activity and fragment

import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;


Info adInfo = null;

try {
     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException e) {
     e.printStackTrace();
} catch (GooglePlayServicesAvailabilityException e) {
     e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
     e.printStackTrace();
}

String AdId = adInfo.getId();

But when i called from pending intent like Package Removed then i want to call the web service at that time i need advertising id but i got null.if you people had done previously please suggest me.thanks in advance.

Upvotes: 1

Views: 8096

Answers (2)

Aneh Thakur
Aneh Thakur

Reputation: 1100

You can try this code and call the bellow method on onCreate

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);
            }
        }
    });
}

Check complete post: how to get AAID programmatically

Upvotes: 1

ceph3us
ceph3us

Reputation: 7474

try use app context

as receivers could use activity context or app context - depends who&how started the receiver

 AdvertisingIdClient.getAdvertisingIdInfo(context.getApplicationContext());

Upvotes: 1

Related Questions