无法获取Android广告ID



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

如果您想获得已发布应用程序的广告id,请使用以下方法:

将这一行添加到渐变文件中;

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();
}
}
});
}

文件中也有一条说明:票据

相关内容

  • 没有找到相关文章

最新更新