[英]Replacement for deprecated EU consent code android admob
目前我正在使用以下代碼在我的 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
那么如何替換這個不推薦使用的代碼呢? 我不贊成這樣做嗎? 如果它已被棄用,那么獲得歐洲用戶同意的類似功能是什么?
昨天我也遇到了這個。 首先,您必須將庫替換為新庫,並按照快速入門中的所有步驟進行操作。 請記住遵循先決條件,尤其是在 adMob 設置上創建 FundingChoices。 你的代碼幾乎是好的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.