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

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




  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
探索全栈前端技术的魅力:HTML+CSS+JS+JQ+Bootstrap网站源码深度解析 在这个数字化时代,构建一个既美观又功能强大的网站成为了许多开发者和企业追逐的目标。本份资源精心汇集了一套完整网站源码,融合了HTML的骨架搭建、CSS的视觉美化、JavaScript的交互逻辑、jQuery的高效操作以及Bootstrap的响应式设计,全方位揭秘了现代网页开发的精髓。 HTML,作为网页的基础,它构建了信息的框架;CSS则赋予网页生动的外观,让设计创意跃然屏上;JavaScript的加入,使网站拥有了灵动的交互体验;jQuery,作为JavaScript的强力辅助,简化了DOM操作与事件处理,让编码更为高效;而Bootstrap的融入,则确保了网站在不同设备上的完美呈现,响应式设计让访问无界限。 通过这份源码,你将: 学习如何高效组织HTML结构,提升页面加载速度与SEO友好度; 掌握CSS高级技巧,如Flexbox与Grid布局,打造适应各种屏幕的视觉盛宴; 理解JavaScript核心概念,动手实现动画、表单验证等动态效果; 利用jQuery插件快速增强用户体验,实现滑动效果、Ajax请求等; 深入Bootstrap框架,掌握移动优先的开发策略,响应式设计信手拈来。 无论是前端开发新手渴望系统学习,还是资深开发者寻求灵感与实用技巧,这份资源都是不可多得的宝藏。立即深入了解,开启你的全栈前端探索之旅,让每一个网页都成为技术与艺术的完美融合!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值