【发布时间】: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