谷歌广告 ID 获取

2019-04-25  本文已影响0人  that_is_this

1. 实现方式一

可参考 :https://blog.csdn.net/qq_33689414/article/details/75135798

    public static String getAIString(Context context) throws Exception {
        try {
            PackageManager pm = context.getPackageManager();
            pm.getPackageInfo("com.android.vending", 0);
        } catch (Exception e) {
            throw e;
        }

        String aid = null;
        try {
            AC connection = new AC();
            Intent intent = new Intent("com.google.android.gms.ads.identifier.service.START");
            intent.setPackage("com.google.android.gms");
            if (context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
                try {
                    AI adInterface = new AI(connection.getBinder());
                    aid = adInterface.getId();
                    return aid;
                } catch (Exception exception) {
                    throw exception;
                } finally {
                    context.unbindService(connection);
                }
            }
        } catch (Exception e) {
            throw e;
        }
        return aid;
    }

    private static final class AC implements ServiceConnection {
        boolean retrieved = false;
        private final LinkedBlockingQueue<IBinder> queue = new LinkedBlockingQueue<IBinder>(1);

        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                this.queue.put(service);
            } catch (InterruptedException localInterruptedException) {
            }
        }

        public void onServiceDisconnected(ComponentName name) {
        }

        public IBinder getBinder() throws InterruptedException {
            if (this.retrieved) throw new IllegalStateException();
            this.retrieved = true;
            return (IBinder) this.queue.take();
        }
    }

    private static final class AI implements IInterface {
        private IBinder binder;

        public AI(IBinder pBinder) {
            binder = pBinder;
        }

        public IBinder asBinder() {
            return binder;
        }

        public String getId() throws RemoteException {
            Parcel data = Parcel.obtain();
            Parcel reply = Parcel.obtain();
            String id;
            try {
                data.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");
                binder.transact(1, data, reply, 0);
                reply.readException();
                id = reply.readString();
            } finally {
                reply.recycle();
                data.recycle();
            }
            return id;
        }
    }

2. 实现方式二

导入 google play services Jar 包

dependencies {
    compile 'com.google.android.gms:play-services:6.5.87'
}

直接调用

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

AdvertisingIdClient.Info info = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
String advertisingId = info.getId();

3. 说明

获取 ID 时消耗时间的,大约耗时 40ms
个人感觉方式一优于方式二

上一篇下一篇

猜你喜欢

热点阅读