替换已弃用的欧盟同意代码 android admob
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。
你的代码差不多好了。
目前我正在使用以下代码在我的 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。 你的代码差不多好了。