Android 获取AAID(Google Advertising ID)(Android广告标识)

您所在的位置:网站首页 手机谷歌广告 Android 获取AAID(Google Advertising ID)(Android广告标识)

Android 获取AAID(Google Advertising ID)(Android广告标识)

2024-07-11 00:50| 来源: 网络整理| 查看: 265

Android 在集成FaceBook广告时,测试时需要添加测试账号,需要使用到Facebook Id、邮箱和AAID(iOS端需要IDFA)

AAID是Android广告标识,通过Google Service可以获取Google Advertising ID(如果没有Google Service就回去不到Google Advertising ID),还需要安装谷歌商店~

/** * Created by lyq * on 2020-05-06 */ public class AdvertisingIdClient { /** * 这个方法是耗时的,不能在主线程调用 */ 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 queue = new LinkedBlockingQueue(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 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; } } }

在kotlin里面调:

Thread(object : Runnable{ override fun run() { var adid = AdvertisingIdClient.getGoogleAdId(this@OPayWayProActivity) } }).start()

原文:

https://blog.csdn.net/qq_33689414/article/details/75135798?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-7&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-7

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3