【问题标题】:Unable to get Android Advertising ID无法获取 Android 广告 ID
【发布时间】:2020-06-12 22:33:27
【问题描述】:

我正在尝试获取 Android 广告 ID,但找不到正确的方法。事实上,我什至无法获得广告 ID 提供者。 isAdvertisingIdProviderAvailable() 总是返回 false。我正在使用带有 8.0 + PlayStore 的三星以及带有 8.1 + Google Play 的模拟器,作为调试版本运行。 我遵循了本指南: https://developer.android.com/training/articles/ad-id 这一定很简单,但我看不到。 感谢您的任何建议。

我创建了一个空白项目,这是我的代码:

public class MainActivity extends AppCompatActivity {
    public static final String TAG="MYTAG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean isProvider = AdvertisingIdClient.isAdvertisingIdProviderAvailable(getApplicationContext());
        Log.i(TAG, "isProviderAvailable:" + isProvider);
    }
}

分级:

 android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "company.com.adidviewer"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
    implementation 'androidx.ads:ads-identifier:1.0.0-alpha04'
    //implementation 'androidx.ads:ads-identifier-common:1.0.0-alpha04'
    //implementation 'androidx.ads:ads-identifier-provider:1.0.0-alpha04'
    // Used for the calls to addCallback() in the snippets on this page.
    //implementation 'com.google.guava:guava:28.0-android'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

清单

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true"/>
</application>

【问题讨论】:

  • 嘿,我遇到了类似的行为,你设法让它工作了吗?
  • 我发现这些行有效,但这是来自非 AndroidX 项目。 AdvertisingIdClient.Info 信息 = AdvertisingIdClient.getAdvertisingIdInfo(context); androidId=info.getId();但是,它们似乎只在主线程中工作。如果从另一个线程调用,我会收到一条关于不再支持全局读取的系统消息。

标签: android google-advertising-id


【解决方案1】:

如果您想获取已发布应用的广告 ID,请使用以下方法:

将此行添加到您的 gradle 文件中;

com.google.android.gms:play-services-ads-identifier:16.0.0

还有这样的代码;

  private void determineAdvertisingInfo(Context context) {

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            try {
                AdvertisingIdClient.Info advertisingIdInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
                // You should check this in case the user disabled it from settings
                if (!advertisingIdInfo.isLimitAdTrackingEnabled()) {
                    String id = advertisingIdInfo.getId();
                    // getUserAttributes(id);
                } else {
                    //If you stored the id you should remove it
                }
            } catch (IOException | GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            }
        }
    });
}

文档中也有一条说明: Note

【讨论】:

  • 这个命令会为你返回什么? boolean isProvider = AdvertisingIdClient.isAdvertisingIdProviderAvailable(getApplicationContext());
  • 我和你一样挣扎了一天。使用不同的设备它总是返回 false。我不知道为什么,但我意识到本指南不适用于获取用户广告 ID。
猜你喜欢
  • 2015-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-14
  • 1970-01-01
相关资源
最近更新 更多