您的位置:首页 > 编程语言 > Go语言

Google Advertising ID 简介以及快速集成和使用

2015-01-04 19:06 603 查看
AdVertising ID (广告ID)

广告id是用户特殊的,独特的,可重置的广告id,由Google Play Service 提供,它为用户更好的控制,为开发人员提供简单、标准的系统继续使用你的应用程序,它用于广告目的的匿名标示符和或者重置起标示符或者退出以利益为基础的Google Play的医用程序。

广告 ID 可以通过简单的API在你的应用程序中实现。

重点开发功能

标准和简单——广告标识是一个标准的一部分,为广告和简单的系统进行分析。

让用户控制——用户可以在任何时候设置他们的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和广告跟踪偏好。

MethodDescription
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 = 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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: