Google Advertising Id 获取

最近挪了一个坑,新的项目需要读取谷歌的AdvertisingId 作为唯一标识,这里记录一下过程中遇到的问题

1: 什么是谷歌AdvertisingId

是谷歌系列sdk 生成的 用户唯一标识

2: 如何取得

搜了一大圈,发现了如下的博文:

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

奈何,这篇文字时间有点久远了,里面提供了两种方式:
第一种要下载google-play-service.jar,第二种是通过进程间通信(IPC),绑定google service里面的一个服务取得的,但是代码要跑在子线程

虽然上面博文里代码是可用的,可但是有个大前提,你手机上装个google服务,但是,国内的各厂商原厂ROM是很少有google服务的,因此就产生了一个新的问题,我没google服务,还要取AdvertgisingId,那是会报错的,
GooglePlayServicesNotAvailableException

fire.jpg

所以继续找资料喽...

找到了一篇StackOverflow
Download Google Play Services JAR

在这里介绍了 google Play Service jar的下载地址,里面还提供了一个play-services-basement-11.8.0.aar的方式,但是无论是jar还是aar,我发现,也都是提供AdvertisingIdClient,没有判断google Service是否可用的api,
最后,我找到了这个:

Google Maps Android API v2 throws GooglePlayServicesNotAvailableException, out of date, SupportMapFragment.getMap() returns null
里面的回答2,给了下面的解释
as described at the bottom of that link: http://developer.android.com/google/play-services/setup.html

and here comes sample code to handle this properly;

int checkGooglePlayServices =    GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (checkGooglePlayServices != ConnectionResult.SUCCESS) {
    // google play services is missing!!!!
    /* Returns status code indicating whether there was an error. 
    Can be one of following in ConnectionResult: SUCCESS, SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, SERVICE_DISABLED, SERVICE_INVALID.
    */
       GooglePlayServicesUtil.getErrorDialog(checkGooglePlayServices, mActivity, 1122).show();
    }

我们点进这个地址: http://developer.android.com/google/play-services/setup.html,是Google Play Sevice的文档,这才是真正解决问题的所在,
里面列出来Google Play Sevice的各个独立依赖,我选择了

Google Mobile Ads   : com.google.android.gms:play-services-ads:17.2.0

然后是怎么判断 isGooglePlayServicesAvailable()
先贴一段原文

Another approach is to use the isGooglePlayServicesAvailable() method. You get a reference to the singleton object that provides this method using GoogleApiAvailability.getInstance(). You might call this method in the onResume()method of the main activity. If the result code is SUCCESS, then the Google Play services APK is up-to-date and you can continue to make a connection. If, however, the result code is SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, or SERVICE_DISABLED, then the user needs to install an update. In this case, call the getErrorDialog() method and pass it the result error code. The method returns a Dialog you should show, which provides an appropriate message about the error and provides an action that takes the user to Google Play Store to install the update.

翻译过来是:
通过GoogleApiAvailability.getInstance()的单例方法,拿到GoogleApiAvailability的引用,我们可以在onResume()里调用此方法,判断google play service 是否可用,如果返回结果是 SUCCESS,代表可用,我们就可以继续取google Advertising Id了,然而,如果返回结果是: SERVICE_MISSING, SERVICE_VERSION_UPDATE_REQUIRED, or SERVICE_DISABLED,等,那么就需要用户安装google play service 的更新包,这是我们调 getErrorDialog() 方法,吧上面的返回结果码传进去,这个方法会返回个dialog,显示了具体错误信息,并能引导用户去google play store 安装或者更新 google play service,不过估计在国内也是gg了

最终的代码如下:


                    var gsmAvaliable = GoogleApiAvailabilityLight.getInstance().isGooglePlayServicesAvailable(this@MainActivity)
                    if (gsmAvaliable == ConnectionResult.SUCCESS) {
                        Executors.newSingleThreadExecutor().execute {
                            try {
                                var adId: String  = AdvertisingIdClient.getAdvertisingIdInfo(this@MainActivity).id
                                runOnUiThread {
                                    //do something
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                            }
                        }
                    } else {
                        GooglePlayServicesUtil.getErrorDialog(gsmAvaliable, this@MainActivity, 0)
                    }

说明一下上面的GoogleApiAvailabilityLightGooglePlayServicesUtil
如果你只依赖了

Google Mobile Ads   : com.google.android.gms:play-services-ads:17.2.0

那么只能用GoogleApiAvailabilityLight,想要使用GooglePlayServicesUtil,还要依赖

Google Actions, Base Client Library com.google.android.gms:play-services-base:16.1.0

好了,关于google Advertising id的获取,暂时到这里,具体的可以去研究一下官方文档_

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 4,221评论 0 6
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 3,601评论 0 1
  • 央行降准,这要是一年前,我是根本不在乎这个消息的,因为我根本就不知道什么是存款准备金率。现在我不得不知道,这是中国...
    沈水之南阅读 26评论 0 0
  • 不知不觉中十月中旬到了,我们迎来了一年一度的运动会。 瞧!我们班的运动员入场了。 他们一个个神采奕奕,精神抖擞,气...
    赵芷悦阅读 43评论 0 0
  • 花开花落 雪覆雪消 漫漫长路 无人一遭 千言万语 化作寂寥 峰回路转 虚若缥缈 乘风破云 雁呵青梢 烈酒青铜 万古...
    季紫清阅读 40评论 1 3