目前我正在使用以下代码在我的 android 应用中显示横幅广告
private ConsentForm form;
private AdView abAdView;
private void checkForConsent() {
try {
ConsentInformation consentInformation = ConsentInformation.getInstance(FirstActivity.this);
ConsentStatus consentStatus = consentInformation.getConsentStatus();
try {
if (consentStatus == ConsentStatus.NON_PERSONALIZED) {
showNonPersonalizedAds();
} else if (consentStatus == ConsentStatus.PERSONALIZED) {
showPersonalizedAds();
} else {
String[] publisherIds = {"my publisher id"};
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
// User's consent status successfully updated.
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();
break;
case NON_PERSONALIZED:
showNonPersonalizedAds();
break;
case UNKNOWN:
if (ConsentInformation.getInstance(getBaseContext())
.isRequestLocationInEeaOrUnknown()) {
requestConsent();
} else {
showPersonalizedAds();
}
break;
default:
break;
}
}
@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
// User's consent status failed to update.
}
});
}
} catch (Exception e) {
String[] publisherIds = {"my publisher id"};
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
// User's consent status successfully updated.
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();
break;
case NON_PERSONALIZED:
showNonPersonalizedAds();
break;
case UNKNOWN:
if (ConsentInformation.getInstance(getBaseContext())
.isRequestLocationInEeaOrUnknown()) {
requestConsent();
} else {
showPersonalizedAds();
}
break;
default:
break;
}
}
@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
// User's consent status failed to update.
}
});
}
}catch (Exception e)
{
try {
if (abAdView != null) {
abAdView.pause();
adContainerView.removeAllViews();
abAdView.destroy();
abAdView = null;
}
}catch (Exception ignored){}
}
}
private void requestConsent() {
URL privacyUrl = null;
try {
privacyUrl = new URL("my privacy URL");
} catch (MalformedURLException ignored) {
}
form = new ConsentForm.Builder(FirstActivity.this, privacyUrl)
.withListener(new ConsentFormListener() {
@Override
public void onConsentFormLoaded() {
// Consent form loaded successfully.
showForm();
}
@Override
public void onConsentFormOpened() {
// Consent form was displayed.
}
@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
switch (consentStatus) {
case PERSONALIZED:
{showPersonalizedAds();break;}
case NON_PERSONALIZED:
case UNKNOWN:
{showNonPersonalizedAds();break;}
}
// Consent form was closed.
}
@Override
public void onConsentFormError(String errorDescription) {
// Consent form error.
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
.build();
form.load();
}
private void showPersonalizedAds() {
try {
abAdView = new AdView(FirstActivity.this);
abAdView.setAdUnitId("my ad unit id");
adContainerView.removeAllViews();
adContainerView.addView(abAdView);
// first of all get ad size
AdSize adSize = getAdSize();
abAdView.setAdSize(adSize);
//banner ad
MobileAds.initialize(FirstActivity.this);
// Step 1 - Create an AdView and set the ad unit ID on it.
ConsentInformation.getInstance(FirstActivity.this)
.setConsentStatus(ConsentStatus.PERSONALIZED);
AdRequest adRequest = new AdRequest.Builder()
.build();
abAdView.loadAd(adRequest);
}catch (Exception e)
{
try {
if (abAdView != null) {
abAdView.pause();
adContainerView.removeAllViews();
abAdView.destroy();
abAdView = null;
}
}catch (Exception ignored){}
}
}
private void showNonPersonalizedAds() {
try{
abAdView = new AdView(FirstActivity.this);
abAdView.setAdUnitId("my ad unit id");
adContainerView.removeAllViews();
adContainerView.addView(abAdView);
//first of all get ad size
AdSize adSize = getAdSize();
abAdView.setAdSize(adSize);
//banner ad
MobileAds.initialize(FirstActivity .this);
ConsentInformation.getInstance(FirstActivity .this)
.setConsentStatus(ConsentStatus.NON_PERSONALIZED);
AdRequest adRequest = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())
.build();
abAdView.loadAd(adRequest);
}catch (Exception e)
{
try {
if (abAdView != null) {
abAdView.pause();
adContainerView.removeAllViews();
abAdView.destroy();
abAdView = null;
}
}catch (Exception ignored){}
}
}
private AdSize getAdSize() {
// Determine the screen width (less decorations)
// to use for the ad width.
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = outMetrics.density;
float adWidthPixels = adContainerView.getWidth();
// If the ad hasn't been laid out,
// default to the full screen width.
if (adWidthPixels == 0) {
adWidthPixels = outMetrics.widthPixels;
}
int adWidth = (int) (adWidthPixels / density);
return AdSize.
getCurrentOrientationAnchoredAdaptiveBannerAdSize(this,
adWidth);
}
private Bundle getNonPersonalizedAdsBundle() {
Bundle extras = new Bundle();
extras.putString("npa", "1");
return extras;
}
private void showForm() {
if (form != null) {
form.show();
}
}
此代码基于: https ://developers.google.com/admob/android/eu-consent
但现在看来,整个代码已被替换为弃用: https ://developers.google.com/admob/ump/android/quick-start
那么如何替换这个不推荐使用的代码呢?我不赞成这样做吗?如果它已被弃用,那么获得欧洲用户同意的类似功能是什么?