转载地址:http://www.2cto.com/kf/201501/366852.html
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和广告跟踪偏好。
Method | Description |
---|---|
public String getId() | Retrieves the advertising ID. |
public boolean isLimitAdTrackingEnabled() | Retrieves whether the user has limit ad tracking enabled or not. |
例子一:
获取ID要放在子线程中,这种方式是要把google-play-service.jar放在项目的lib下,整个jar大概有3M多,还有一种不需要集成jar的方式见例子二。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
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;
}
}
}</ibinder></ibinder>
|
使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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();
|