- MobileAds.initialize(applicationContext, adId);
-
- // Store the publisher ID in a string array.
- String[] publisherIds = {"pub-5962503714887045"};
-
- // Check to see if consent is needed in Europe to comply with the GDPR.
- ConsentInformation consentInformation = ConsentInformation.getInstance(applicationContext);
- consentInformation.requestConsentInfoUpdate(publisherIds, new ConsentInfoUpdateListener() {
- @Override
- public void onConsentInfoUpdated(ConsentStatus consentStatus) {
- if (consentStatus == ConsentStatus.UNKNOWN) { // The user has not yet consented to ads.
- // Display the ad consent dialog.
- DialogFragment adConsentDialogFragment = new AdConsentDialog();
- adConsentDialogFragment.show(fragmentManager, "Ad Consent");
- } else { // The user has consented to ads.
- // Indicate the user is under age, which disables personalized advertising and remarketing. https://developers.google.com/admob/android/eu-consent
- consentInformation.setTagForUnderAgeOfConsent(true);
-
- // Load an ad.
- loadAd(view, applicationContext, adId);
- }
- }
-
- @Override
- public void onFailedToUpdateConsentInfo(String reason) { // The user is not in Europe.
- // Indicate the user is under age, which disables personalized advertising and remarketing. https://developers.google.com/admob/android/eu-consent
- consentInformation.setTagForUnderAgeOfConsent(true);
-
- // Load an ad.
- loadAd(view, applicationContext, adId);
- }
- });
+ MobileAds.initialize(applicationContext, googleAppId);
+
+ // Initialize the bookmarks database helper. The `0` specifies a database version, but that is ignored and set instead using a constant in `AdConsentDatabaseHelper`.
+ AdConsentDatabaseHelper adConsentDatabaseHelper = new AdConsentDatabaseHelper(applicationContext, null, null, 0);
+
+ // Check to see if consent has been granted.
+ boolean adConsentGranted = adConsentDatabaseHelper.isGranted();
+
+ // Display the ad consent dialog if needed.
+ if (!adConsentGranted) { // Ad consent has not been granted.
+ // Display the ad consent dialog.
+ DialogFragment adConsentDialogFragment = new AdConsentDialog();
+ adConsentDialogFragment.show(fragmentManager, "Ad Consent");
+ } else { // Ad consent has been granted.
+ // Load an ad.
+ loadAd(view, applicationContext, adUnitId);
+ }