如何获取Gaid,Android Advertising ID 简介以及快速集成和使用

转载地址: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和广告跟踪偏好。

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的方式见例子二。

?
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();


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值