5

我正在尝试获取 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>
4

1 回答 1

6

如果您想获取已发布应用的广告 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();
            }
        }
    });
}

文档中也有一条说明: 注意

于 2020-04-11T12:46:03.407 回答