Android-谷歌规范-获取GAID

谷歌规范中,涉及广告部分的需要加上gaid,整理了一下日常获取gaid的方法.

方法1. 通过谷歌server的jar包来获取,整个jar包太大.12mb.放弃

方法2. 通过basement的jar包来获取.比较小.300来k的样子,很容易获取(注意:此方法需要在子线程执行)

获取jar包

                try {
                    AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context.getApplicationContext());
                    GAID = advertisingIdInfo.getId();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                }

这里需要注意的是,需要在AndroidManifest.xml中加上

    <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

其中google_play_services_version为

<integer name="google_play_services_version">12211000</integer>

 方法3. 通过跨进程服务来获得..此方法要求手机上必须装谷歌服务.这里声明.只要安装了谷歌商店.是一定会安装谷歌服务的.

                try {
                    AdvertisingIdClient.AdInfo adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                    GAID = adInfo.getId();
                } catch (Exception e) {
                    e.printStackTrace();
                }

附上跨进程代码.

public class AdvertisingIdClient {

    public static final class AdInfo {

        private
        final String advertisingId;

        private
        final boolean limitAdTrackingEnabled;


        AdInfo(String advertisingId, boolean
                limitAdTrackingEnabled) {

            this.advertisingId = advertisingId;

            this.limitAdTrackingEnabled = limitAdTrackingEnabled;

        }

        public String getId() {

            return
                    this.advertisingId;

        }

        public boolean isLimitAdTrackingEnabled() {

            return
                    this.limitAdTrackingEnabled;

        }

    }

    public static AdInfo getAdvertisingIdInfo(Context context) throws Exception {

        if (Looper.myLooper() == Looper.getMainLooper())
            throw new IllegalStateException("Cannot be called from the main thread");
        try {

            PackageManager pm = context.getPackageManager();

            pm.getPackageInfo("com.android.vending", 0);

        } catch (Exception e) {
            throw e;
        }


        AdvertisingConnection connection = new
                AdvertisingConnection();

        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 {

                AdvertisingInterface adInterface = new
                        AdvertisingInterface(

                        connection.getBinder());

                AdInfo adInfo = new
                        AdInfo(adInterface.getId(),

                        adInterface.isLimitAdTrackingEnabled(true));

                return
                        adInfo;

            } catch
                    (Exception exception) {

                throw
                        exception;

            } finally {

                context.unbindService(connection);

            }

        }

        throw new IOException("Google Play connection failed");

    }


    private
    static final class AdvertisingConnection implements

            ServiceConnection {

        boolean
                retrieved = false;

        private final LinkedBlockingQueue<IBinder> queue = new LinkedBlockingQueue<>(1);


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

            } catch (InterruptedException ignored) {
            }
        }


        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 AdvertisingInterface implements
            IInterface {

        private
        IBinder binder;


        public AdvertisingInterface(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;

        }


        public boolean isLimitAdTrackingEnabled(boolean paramBoolean) throws
                RemoteException {

            Parcel data = Parcel.obtain();

            Parcel reply = Parcel.obtain();

            boolean limitAdTracking;

            try {

                data.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");

                data.writeInt(paramBoolean ? 1
                        : 0);

                binder.transact(2, data, reply, 0);

                reply.readException();

                limitAdTracking = 0
                        != reply.readInt();

            } finally {

                reply.recycle();

                data.recycle();

            }

            return limitAdTracking;

        }

    }

}

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值