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

来源:互联网 发布:意大利面料 知乎 编辑:程序博客网 时间:2024/06/06 13:56

Google 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和广告跟踪偏好。

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

[java] view plaincopy
  1. import com.google.android.gms.ads.identifier.AdvertisingIdClient;  
  2. import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;  
  3. import com.google.android.gms.common.GooglePlayServicesAvailabilityException;  
  4. import com.google.android.gms.common.GooglePlayServicesNotAvailableException;  
  5. import java.io.IOException;  
  6. ...  
  7.   
  8. // Do not call this function from the main thread. Otherwise,   
  9. // an IllegalStateException will be thrown.  
  10. public void getIdThread() {  
  11.   
  12.   Info adInfo = null;  
  13.   try {  
  14.     adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);  
  15.   
  16.   } catch (IOException e) {  
  17.     // Unrecoverable error connecting to Google Play services (e.g.,  
  18.     // the old version of the service doesn't support getting AdvertisingId).  
  19.    
  20.   } catch (GooglePlayServicesAvailabilityException e) {  
  21.     // Encountered a recoverable error connecting to Google Play services.   
  22.   
  23.   } catch (GooglePlayServicesNotAvailableException e) {  
  24.     // Google Play services is not available entirely.  
  25.   }  
  26.   final String id = adInfo.getId();  
  27.   final boolean isLAT = adInfo.isLimitAdTrackingEnabled();  
  28. }  

例子二:

不需要集成google-play-service.jar

这种方式就要求手机本身安装了Google Play Service,这里采用绑定Service和夸进程通信的方式获取广告ID。

创建一个类 AdvertisingIdClient.java

[java] view plaincopy
  1. public class AdvertisingIdClient {  
  2.     public static final class AdInfo {  
  3.         private final String advertisingId;  
  4.         private final boolean limitAdTrackingEnabled;     
  5.   
  6.         AdInfo(String advertisingId, boolean limitAdTrackingEnabled) {  
  7.             this.advertisingId = advertisingId;  
  8.             this.limitAdTrackingEnabled = limitAdTrackingEnabled;  
  9.         }  
  10.   
  11.         public String getId() {  
  12.             return this.advertisingId;  
  13.         }  
  14.   
  15.         public boolean isLimitAdTrackingEnabled() {  
  16.             return this.limitAdTrackingEnabled;  
  17.         }  
  18.     }  
  19.   
  20.     public static AdInfo getAdvertisingIdInfo(Context context) throws Exception {  
  21.         if (Looper.myLooper() == Looper.getMainLooper())  
  22.             throw new IllegalStateException(  
  23.                     "Cannot be called from the main thread");  
  24.   
  25.         try {  
  26.             PackageManager pm = context.getPackageManager();  
  27.             pm.getPackageInfo("com.android.vending"0);  
  28.         } catch (Exception e) {  
  29.             throw e;  
  30.         }  
  31.   
  32.         AdvertisingConnection connection = new AdvertisingConnection();  
  33.         Intent intent = new Intent(  
  34.                 "com.google.android.gms.ads.identifier.service.START");  
  35.         intent.setPackage("com.google.android.gms");  
  36.         if (context.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {  
  37.             try {  
  38.                 AdvertisingInterface adInterface = new AdvertisingInterface(  
  39.                         connection.getBinder());  
  40.                 AdInfo adInfo = new AdInfo(adInterface.getId(),  
  41.                         adInterface.isLimitAdTrackingEnabled(true));  
  42.                 return adInfo;  
  43.             } catch (Exception exception) {  
  44.                 throw exception;  
  45.             } finally {  
  46.                 context.unbindService(connection);  
  47.             }  
  48.         }  
  49.         throw new IOException("Google Play connection failed");  
  50.     }  
  51.   
  52.     private static final class AdvertisingConnection implements  
  53.             ServiceConnection {  
  54.         boolean retrieved = false;  
  55.         private final LinkedBlockingQueue<IBinder> queue = new LinkedBlockingQueue<IBinder>(  
  56.                 1);  
  57.   
  58.         public void onServiceConnected(ComponentName name, IBinder service) {  
  59.             try {  
  60.                 this.queue.put(service);  
  61.             } catch (InterruptedException localInterruptedException) {  
  62.             }  
  63.         }  
  64.   
  65.         public void onServiceDisconnected(ComponentName name) {  
  66.         }  
  67.   
  68.         public IBinder getBinder() throws InterruptedException {  
  69.             if (this.retrieved)  
  70.                 throw new IllegalStateException();  
  71.             this.retrieved = true;  
  72.             return (IBinder) this.queue.take();  
  73.         }  
  74.     }  
  75.   
  76.     private static final class AdvertisingInterface implements IInterface {  
  77.         private IBinder binder;  
  78.   
  79.         public AdvertisingInterface(IBinder pBinder) {  
  80.             binder = pBinder;  
  81.         }  
  82.   
  83.         public IBinder asBinder() {  
  84.             return binder;  
  85.         }  
  86.   
  87.         public String getId() throws RemoteException {  
  88.             Parcel data = Parcel.obtain();  
  89.             Parcel reply = Parcel.obtain();  
  90.             String id;  
  91.             try {  
  92.                 data.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");  
  93.                 binder.transact(1, data, reply, 0);  
  94.                 reply.readException();  
  95.                 id = reply.readString();  
  96.             } finally {  
  97.                 reply.recycle();  
  98.                 data.recycle();  
  99.             }  
  100.             return id;  
  101.         }  
  102.   
  103.         public boolean isLimitAdTrackingEnabled(boolean paramBoolean)  
  104.                 throws RemoteException {  
  105.             Parcel data = Parcel.obtain();  
  106.             Parcel reply = Parcel.obtain();  
  107.             boolean limitAdTracking;  
  108.             try {  
  109.                 data.writeInterfaceToken("com.google.android.gms.ads.identifier.internal.IAdvertisingIdService");  
  110.                 data.writeInt(paramBoolean ? 1 : 0);  
  111.                 binder.transact(2, data, reply, 0);  
  112.                 reply.readException();  
  113.                 limitAdTracking = 0 != reply.readInt();  
  114.             } finally {  
  115.                 reply.recycle();  
  116.                 data.recycle();  
  117.             }  
  118.             return limitAdTracking;  
  119.         }  
  120.     }  
  121. }  

使用:

[java] view plaincopy
  1. new Thread(new Runnable() {  
  2.             public void run() {  
  3.                 try {  
  4.                     AdInfo adInfo = AdvertisingIdClient  
  5.                             .getAdvertisingIdInfo(MainActivity.this);  
  6.                     advertisingId = adInfo.getId();  
  7.                     optOutEnabled = adInfo.isLimitAdTrackingEnabled();  
  8.                     // Log.i("ABC", "advertisingId" + advertisingId);  
  9.                     // Log.i("ABC", "optOutEnabled" + optOutEnabled);  
  10.                 } catch (Exception e) {  
  11.                     e.printStackTrace();  
  12.                 }  
  13.                 mHandler.sendEmptyMessage(HANDEL_ADID);  
  14.             }  
  15.         }).start();  
0 0
原创粉丝点击