GoogleAdvertisingID简介以及快速集成和使用
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:259
AdVertising ID (告白ID)
告白id是用户特别的,独特的,可重置的告白id,由Google Play Service 供给,它为用户更好的┞菲握,为开辟人员供给简单、标准的体系持续应用你的应用法度榜样,它用于告白目标的匿名标示符和或者重置起标示符或者退出以好处为基本的Google Play的医用法度榜样。
告白 ID 可以经由过程简单的API在你的应用法度榜样中实现。
重点开辟功能
标准和R单——告白标识是一个标准的一部分,为告白和R单的体系进行分析。
让用户控制——用户可以在任何时刻设置他们的ID或者退出那些以好处为基本的告白大年夜谷歌的应用法度榜样,他们的偏罕善于告白公司的告白ID。
开端
获取Google Play Service SDK——大年夜下载好的Android SDK 的 Extras 目次下找 library 下面的google-play-service.jar
浏览文档和例子——文档例子
注释
作为提示,请留意,大年夜2014-08-01,新的应用法度榜样和应用法度榜样的更新经由过程谷歌晃荡必须在任何告白目标的任何其他持久标识符代替应用告白ID设备上支撑的告白
若何检查您的应用法度榜样的合规性经由过程开辟控制台,或在相干的开辟政策变更的细节,请看在谷歌游戏开辟者赞助中间告白ID的参考
应用告白ID
告白标识是一个独特的但用户复位字符串标识符,让收集告白和其他应用法度榜样的匿名标识一个用户。用户的告白ID是经由过程API供给的办事供给给应用法度榜样的在Google Play Service中。
用户可以在任何时刻设置他们的告白ID,大年夜谷歌设置应用法度榜样在设毕喔赡告白部分的权力。大年夜雷同的应用法度榜样,用户还可以选择有针对性的告白的告白ID的基本上,来设置合适的告白跟踪偏好。当用户选择了有针对性的告白,这个告白跟踪偏好是供给给应用法度榜样经由过程谷歌播放办事API。
应用法度榜样应用告白必须尊检查并尊敬用户的习惯和偏好跟踪,还请留意,任何应用告白id的应用法度榜样都必须尊敬Google的开辟内容政策条目。
ID 格式
Google Play Service 的API 裸露和用户的 ID 为 UUID 的字符串格式。
须要
告白 ID API支撑Google Play Service 4.0+ 的设备
对具体设备的支撑是基于设备安装的Google Paly Service 的版本
用户的告白ID和告白跟踪优先获得
如不雅你应用法度榜样想要应用告白ID,你的设备就必须安装Google Play Service
告白ID的API可在com.google.android.gms.ads.identifier包在Google Play Service的的库中。获得用户的告白ID和跟踪偏好,调用办法getadvertisingidinfo(),它返回一个advertisingidclient信息封装。用户当前的告白ID和跟踪偏好。
getadvertisingidinfo()办法的壅塞调用,所以你不克不及说它在主线程(UI线程)。如不雅在主线程,该办法抛出illegalstateexception异常。
一旦你取回advertisingidclient对象,您可以应用它的getid()和islimitadtrackingenabled()办法拜访的告白ID和告白跟踪偏好。
Method Description
public String getId()
Retrieves the advertising ID.
public boolean isLimitAdTrackingEnabled()
Retrieves whether the user has limit ad tracking enabled or not.
告白ID API不包含“复位”的办法。只有效户可以启动复位本身的告白ID,在Google
Play Service设置中。
例子一:
获取ID要放在子线程中,这种方法是要把google-play-service.jar放在项目标lib下,全部jar大年夜概有3M多,还有一种不须要集成jar的方法见例子二。
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
import com.google.android.gms.common.GooglePlayServicesAvailabilityException;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import java.io.IOException;
...
// Do not call this function from the main thread. Otherwise,
// an IllegalStateException will be thrown.
public void getIdThread() {
Info adInfo = null;
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
} catch (IOException e) {
// Unrecoverable error connecting to Google Play services (e.g.,
// the old version of the service doesn't support getting AdvertisingId).
} catch (GooglePlayServicesAvailabilityException e) {
// Encountered a recoverable error connecting to Google Play services.
} catch (GooglePlayServicesNotAvailableException e) {
// Google Play services is not available entirely.
}
final String id = adInfo.getId();
final boolean isLAT = adInfo.isLimitAdTrackingEnabled();
}例子二:
不须要集成google-play-service.jar怎么获取呢?
这种方法就请求手机本身安装了Google Play Service,这里采取绑定Service和夸过程通信的方法获取告白ID。
创建一个类 AdvertisingIdClient.java
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<IBinder>(
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 (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 = http://www.sjsjw.com/100/000336MYM017845/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;
}
}
}
应用:
new Thread(new Runnable() {
public void run() {
try {
AdInfo adInfo = AdvertisingIdClient
.getAdvertisingIdInfo(MainActivity.this);
advertisingId = adInfo.getId();
optOutEnabled = adInfo.isLimitAdTrackingEnabled();
// Log.i("ABC", "advertisingId" + advertisingId);
// Log.i("ABC", "optOutEnabled" + optOutEnabled);
} catch (Exception e) {
e.printStackTrace();
}
mHandler.sendEmptyMessage(HANDEL_ADID);
}
}).start();








