Android 获取手机的唯一ID(针对有Google服务的手机)

前言

相信很多朋友在开发中都会遇到这种问题,怎么获取设备的唯一ID,有很多方法可以实现。但是,其中都会有些许问题。这里小可主要讲解一下,如何获取设备的唯一ID。当然,针对的是安装有Google服务的Android手机

首先

public class DeviceUtils {

/**
 * 这个方法是耗时的,不能在主线程调用
 */
public static String getGoogleAdId(Context context) throws Exception {
    if (Looper.getMainLooper() == Looper.myLooper()) {
        return "Cannot call in the main thread, You must call in the other thread";
    }
    PackageManager pm = context.getPackageManager();
    pm.getPackageInfo("com.android.vending", 0);
    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());
            return adInterface.getId();
        } finally {
            context.unbindService(connection);
        }
    }
    return "";
}

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 ex) {
            ex.printStackTrace();
        }
    }

    public void onServiceDisconnected(ComponentName name) {
    }

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

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

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

}

其次

如何使用?直接获取会出问题,需要在子线程中获取

Executors.newSingleThreadExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                       String mAdID = DeviceUtils.getGoogleAdId(getApplicationContext());
                        if (!TextUtils.isEmpty(mAdID)) {
                            init();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

最后

好了,以上就是获取设置唯一标识的方法,在这里推荐一个交流群:493180098,欢迎来提问,吹水。