我在我的项目中实施了谷歌同意sdk,直到我想在用户离开欧洲经济区时正常展示广告,并在他进入欧洲经济区时显示个性化或非个性化广告。但更改后,代码不会给我任何结果。
这是我的代码:
public class MainActivity extends AppCompatActivity {
private String TAG = this.getClass().getSimpleName();
ConsentForm form;
TextView textView;
private AdView mAdView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, "ca-app-pub-xxxxxxx");
showPersonalizedAds();
textView = findViewById(R.id.simo2);
checkForConsent();
ConsentInformation.getInstance(MainActivity.this).setDebugGeography(DebugGeography.DEBUG_GEOGRAPHY_EEA);
}
private void checkForConsent() {
ConsentInformation consentInformation = ConsentInformation.getInstance(MainActivity.this);
String[] publisherIds = {"pub-xxxxx"};
ConsentInformation.getInstance(MainActivity.this).addTestDevice("xxxxxx");
consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
@Override
public void onConsentInfoUpdated(ConsentStatus consentStatus) {
// User's consent status successfully updated.
switch (consentStatus) {
case PERSONALIZED:
Log.d(TAG, "Showing Personalized ads");
showPersonalizedAds();
break;
case NON_PERSONALIZED:
Log.d(TAG, "Showing Non-Personalized ads");
showNonPersonalizedAds();
break;
case UNKNOWN:
Log.d(TAG, "Requesting Consent");
if (ConsentInformation.getInstance(getBaseContext())
.isRequestLocationInEeaOrUnknown()) {
requestConsent();
} else {
showPersonalizedAds();
}
break;
default:
break;
}
}
@Override
public void onFailedToUpdateConsentInfo(String errorDescription) {
// User's consent status failed to update.
}
});
// on the onCreate
}
private void requestConsent() {
URL privacyUrl = null;
try {
// TODO: Replace with your app's privacy policy URL.
privacyUrl = new URL("http://qisasprivacy.blogspot.com/");
} 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.
Log.d(TAG, "Requesting Consent: onConsentFormLoaded");
showForm();
}
@Override
public void onConsentFormOpened() {
// Consent form was displayed.
Log.d(TAG, "Requesting Consent: onConsentFormOpened");
}
@Override
public void onConsentFormClosed(
ConsentStatus consentStatus, Boolean userPrefersAdFree) {
// Consent form was closed.
Log.d(TAG, "Requesting Consent: onConsentFormClosed");
if (userPrefersAdFree) {
// Buy or Subscribe
Log.d(TAG, "Requesting Consent: User prefers AdFree");
} else {
Log.d(TAG, "Requesting Consent: Requesting consent again");
switch (consentStatus) {
case PERSONALIZED:
showPersonalizedAds();break;
case NON_PERSONALIZED:
showNonPersonalizedAds();break;
case UNKNOWN:
showNonPersonalizedAds();break;
}
}
}
@Override
public void onConsentFormError(String errorDescription) {
// Consent form error.
Log.d(TAG, "Requesting Consent: onConsentFormError. Error - " + errorDescription);
}
})
.withPersonalizedAdsOption()
.withNonPersonalizedAdsOption()
.build();
// load the form so we can call .show on it after
form.load();
}
private void showPersonalizedAds() {
AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxxxxx") // Todo: Remove in Release build
.build();
mAdView.loadAd(adRequest);
}
private void showNonPersonalizedAds() {
AdView mAdView = findViewById(R.id.adView);
Bundle extras = new Bundle();
extras.putString("npa", "1");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxxxxxx") // Todo: Remove in Release build
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.build();
mAdView.loadAd(adRequest);
}
private void showForm() {
if (form == null) {
Log.d(TAG, "Consent form is null");
}
if (form != null) {
Log.d(TAG, "Showing consent form");
form.show();
} else {
Log.d(TAG, "Not Showing consent form");
}
}
}
请提供任何解决方案