AdMob广告可在我的应用程序中使用,我希望在打开应用程序时显示同意书,来自欧洲的用户将选择个性化或非个性化广告,而其他用户将不会看到此表单。 我的代码在所有经过测试的设备上都能正常工作,但是问题是在某些设备上代码可以工作,但是对话框未显示。
我还在朋友的计算机上尝试了此代码,并且在所有设备上都调用了他的表格。
下面,我将显示代码:
private ConsentForm form;
@Override
protected void onCreate(Bundle savedInstanceState) {
ConsentInformation consentInformation = ConsentInformation.getInstance(getApplicationContext());
String[] publisherIds = {getString(R.string.app_id)};
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
// User's consent status successfully updated.
boolean inEEA = ConsentInformation.getInstance(getApplicationContext()).isRequestLocationInEeaOrUnknown();
if (inEEA){
Toast.makeText(MainActivity.this, consentStatus.toString(), Toast.LENGTH_SHORT).show();
if(consentStatus == consentStatus.PERSONALIZED){
}else if(consentStatus == consentStatus.NON_PERSONALIZED){
Bundle extras = new Bundle();
extras.putString("npa", "1");
AdRequest request = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
}else{ //here code form
URL privacyUrl = null;
try {
// TODO: Replace with your app's privacy policy URL.
privacyUrl = new URL(getString(R.string.privacy));
} catch (MalformedURLException e) {
e.printStackTrace();
// Handle error.
}
form = new ConsentForm.Builder(MainActivity.this, privacyUrl)
.withListener(new ConsentFormListener() {
@Override
public void onConsentFormLoaded() {
// Consent form loaded successfully.
form.show();
}
@Override
public void onConsentFormOpened() {
// Consent form was displayed.
}
@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
// Consent form was closed.
if (consentStatus == consentStatus.NON_PERSONALIZED){
Bundle extras = new Bundle();
extras.putString("npa", "1");
AdRequest request = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
}
}
@Override
public void onConsentFormError(String errorDescription) {
// Consent form error.
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
//.withAdFreeOption()
.build();
form.load();
}//here end code form
}else{
//Not Europe
}//end inEEA
}
@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
// User's consent status failed to update.
}
});}