如何在Android Java中获取Google广告标识符(Google Ad Id)

huangapple go评论82阅读模式
英文:

How to get Google Ad Id in android Java

问题

我正在尝试在Android Java中获取Google广告ID(GAID),但是我遇到了以下异常:

androidx.ads.identifier.AdvertisingIdNotAvailableException: 没有可用的兼容的AndroidX广告ID提供程序。

我的代码

ListenableFuture<AdvertisingIdInfo> adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
System.out.println("adinfo" + adInfo.toString());
try {
  String id = adInfo.get().getId();
  System.out.println("adod" + id);
} catch (ExecutionException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

我添加的依赖项

implementation 'com.google.android.gms:play-services-ads:19.4.0'
implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'

在清单文件中我添加了

<meta-data
    android:name="com.google.android.gms.ads.AD_MANAGER_APP"
    android:value="true"/>

我已经尝试了许多解决方案,但都没有用。请帮助我解决这个问题。

英文:

I am trying to get the google ad id (GAID)in android java but I am getting exeception like:

androidx.ads.identifier.AdvertisingIdNotAvailableException: No compatible AndroidX Advertising ID Provider available.

My Code

ListenableFuture&lt;AdvertisingIdInfo&gt; adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
    System.out.println(&quot;adinfo&quot;+adInfo.toString());
    try {
      String id =   adInfo.get().getId();

      System.out.println(&quot;adod&quot;+id);
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

And dependienced i added

  implementation &#39;com.google.android.gms:play-services-ads:19.4.0&#39;
        implementation &#39;androidx.ads:ads-identifier:1.0.0-alpha04&#39;

In Manifest I added

  &lt;meta-data
                android:name=&quot;com.google.android.gms.ads.AD_MANAGER_APP&quot;
                android:value=&quot;true&quot;/&gt;

I have tried many solutions but it is not useful. Please help me out to solve the issue.

答案1

得分: 5

在AndroidX中,gaid依赖似乎存在问题。

许多人(包括我)已经遵循了Google的广告ID文档,并报告了相同的问题。此外,Google关于这个问题的文档似乎已经过时,因为示例代码片段是错误的(例如,函数addCallback缺少一个参数)。

解决方案是在你的Gradle文件中添加这个依赖项,而不是你提到的那些:

implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'

然后你可以通过以下方式获取广告ID

            AdvertisingIdClient.Info idInfo;

            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                String advertisingId = idInfo.getId();

                //对ID进行操作

            } catch (IOException e) {
                e.printStackTrace();

            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();

            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }

如果你想仅在用户未选择个性化广告时使用此ID,你可以在检索到ID信息后添加以下代码:

     //如果用户在设备设置中未选择个性化广告
     if(idInfo.isLimitAdTrackingEnabled()) {
         //对ID进行操作
     }

编辑:上述提到的Google文档实际上有一个提示,鼓励你使用我提到的广告标识符库:

如何在Android Java中获取Google广告标识符(Google Ad Id)

英文:

The gaid dependency in androidx seems to be at fault.

Many people (including me) have followed Google's ad ID documentation and have reported the same issue. Also, Google's documentation on this matter appears outdated since the examplatory code snippet is wrong (e.g. function addCallback is missing a parameter).

The solution is to add this dependency in your gradle file instead of the ones you mentioned:

implementation &#39;com.google.android.gms:play-services-ads-identifier:17.0.0&#39;

You can then fetch the advertising id this way:

            AdvertisingIdClient.Info idInfo;

            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                String advertisingId = idInfo.getId();

                //do sth with the id

            } catch (IOException e) {
                e.printStackTrace();

            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();

            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }

If you want to use the id only when the user hasn't opted out of personalised ads, you can add this after retrieving the id info:

     //if user hasn&#39;t opted out of ads in device settings
     if(idInfo.isLimitAdTrackingEnabled()) {
         //do sth with the id
     }

Edit: The mentioned Google's documentation actually has a note that encourages you to use the ads identifier library I mentioned:

如何在Android Java中获取Google广告标识符(Google Ad Id)

huangapple
  • 本文由 发表于 2020年9月27日 19:20:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64087871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定