设置 Google Play 服务 | Google Play services | Google Developers
方式一
点击上方链接,打开Google PlayServices官网
找到谷歌服务依赖项
两者都可以使用!
play-services-ads-identifier 代替 play-services-ads,只导入广告 ID 类,而不导入 Google Mobile Ads/AdMob,减少包体积。
注意:getAdvertisingIdInfo 方法是耗时的,不能在主线程调用
private void getGoogleAdIdTwo() { Executors.newCachedThreadPool().execute(() -> { try { AdvertisingIdClient.Info idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context); if (idInfo != null) { String adid = idInfo.getId(); if (mHandler != null) { Message message = mHandler.obtainMessage(2, adid); mHandler.sendMessage(message); } } } catch (IOException e) { e.printStackTrace(); } catch (GooglePlayServicesNotAvailableException e) { e.printStackTrace(); } catch (GooglePlayServicesRepairableException e) { e.printStackTrace(); } }); }
方式二
不需要依赖,手机本身安装了Google Play Service,采用绑定Service和夸进程通信的方式获取广告ID。
AdvertisingConnection类
public class AdvertisingConnection implements ServiceConnection { boolean retrieved = false; private final LinkedBlockingQueue queue = new LinkedBlockingQueue(1); @Override public void onServiceConnected(ComponentName name, IBinder service) { Logger.outPut("onServiceConnected ComponentName = " + name); try { this.queue.put(service); } catch (InterruptedException e) { throw new RuntimeException(e); } } @Override public void onServiceDisconnected(ComponentName name) { Logger.outPut("onServiceDisconnected ComponentName = " + name); } public IBinder getBinder() throws InterruptedException { if (this.retrieved) { throw new IllegalStateException(); } this.retrieved = true; return (IBinder) this.queue.take(); } }
AdvertisingInterfaceImpl类
public class AdvertisingInterfaceImpl implements IInterface { private final String INTERFACE_NAME = "com.google.android.gms.ads.identifier.internal.IAdvertisingIdService"; private IBinder binder; public AdvertisingInterfaceImpl(IBinder binder) { this.binder = binder; } @Override public IBinder asBinder() { return binder; } public String getId() throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); String id; try { data.writeInterfaceToken(INTERFACE_NAME); 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(INTERFACE_NAME); data.writeInt(paramBoolean ? 1 : 0); binder.transact(2, data, reply, 0); reply.readException(); limitAdTracking = 0 != reply.readInt(); } finally { reply.recycle(); data.recycle(); } return limitAdTracking; } }
GoogleAdvertisingIdClient类
public class GoogleAdvertisingIdClient { private static final String GOOGLE_PACKAGE_NAME = "com.google.android.gms"; private static final String GOOGLE_ACTION_NAME = "com.google.android.gms.ads.identifier.service.START"; /** * 不可继承,不允许修改 */ public static final class AdInfo { //只允许赋值一次,赋值后不再改变 private final String adId; private final boolean adTrackingEnable; //声明变量不赋初值,构造函数中对变量赋初值。 public AdInfo(String adId, boolean adTrackingEnable) { this.adId = adId; this.adTrackingEnable = adTrackingEnable; } public String getAdId() { return adId; } public boolean isAdTrackingEnable() { return adTrackingEnable; } } public static AdInfo getAdInfo(Context context) throws Exception { if (Looper.myLooper() == Looper.getMainLooper()) { throw new IllegalAccessException("cannot be called from the main thread"); } PackageManager packageManager = context.getPackageManager(); packageManager.getPackageInfo("com.android.vending", 0); AdvertisingConnection connection = new AdvertisingConnection(); Intent intent = new Intent(GOOGLE_ACTION_NAME); intent.setPackage(GOOGLE_PACKAGE_NAME); if (context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) { try { AdvertisingInterfaceImpl adInterface = new AdvertisingInterfaceImpl(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 void getGoogleAdIdOne() { Executors.newCachedThreadPool().execute(() -> { try { GoogleAdvertisingIdClient.AdInfo adInfo = GoogleAdvertisingIdClient.getAdInfo(context); if (adInfo != null) { String adId = adInfo.getAdId(); boolean adTrackingEnable = adInfo.isAdTrackingEnable(); Logger.outPut("adId = " + adId); Logger.outPut("adTrackingEnable = " + adTrackingEnable); if (mHandler != null) { Message message = mHandler.obtainMessage(1, adId); mHandler.sendMessage(message); } } } catch (Exception e) { e.printStackTrace(); } }); }