Невозможно получить Android Рекламный идентификатор - PullRequest
1 голос
/ 28 февраля 2020

Я экспериментирую с получением Android Рекламного идентификатора, но не могу найти правильный путь. На самом деле, я даже не могу получить рекламный идентификатор провайдера. isAdvertisingIdProviderAvailable () всегда возвращает false. Я использую Samsung с 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);
    }
}

Gradle:

 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>

1 Ответ

0 голосов
/ 11 апреля 2020

Если вы хотите получить рекламный идентификатор для вашего опубликованного приложения, используйте следующий подход:

Добавьте эту строку в ваш файл 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();
            }
        }
    });
}

В документе также есть примечание, указывающее это: Примечание

...