Android-Google仕様-GAID取得

6287 ワード

Google仕様では、広告部分に関連してgaidを加え、日常的にgaidを取得する方法を整理しました。
方法1.Google serverのjarパッケージを通じて取得し、全体のjarパッケージが大きすぎます。12 mb.放棄します。
方法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();
                }
ここで注意したいのは、Android Manifest.xmlに加えてください。
    
中google_えんごを打つservices.versionは
12211000
 方法3.プロセスをまたいでのサービスで得られます。この方法は携帯電話にGoogleのサービスをインストールしなければならないことを要求します。ここで声明します。Googleストアを設置したら、必ずGoogleのサービスをインストールします。
                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 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;

        }

    }

}