Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample.app;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- import com.google.ads.consent.ConsentForm;
- import com.google.ads.consent.ConsentFormListener;
- import com.google.ads.consent.ConsentInfoUpdateListener;
- import com.google.ads.consent.ConsentInformation;
- import com.google.ads.consent.ConsentStatus;
- import java.net.MalformedURLException;
- import java.net.URL;
- import static com.google.ads.consent.ConsentStatus.NON_PERSONALIZED;
- import static com.google.ads.consent.ConsentStatus.PERSONALIZED;
- public class MainActivity extends AppCompatActivity {
- private String TAG = this.getClass().getSimpleName();
- private TextView textView;
- private ConsentForm form;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView = findViewById(R.id.text);
- checkForConsent();
- }
- private void checkForConsent() {
- ConsentInformation consentInformation = ConsentInformation.getInstance(MainActivity.this);
- String[] publisherIds = {"pub-id-from-publishers-like-admob"};
- 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.
- }
- });
- }
- private void requestConsent() {
- URL privacyUrl = null;
- try {
- // TODO: Replace with your app's privacy policy URL.
- privacyUrl = new URL("https://your.privacy.url/");
- } 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) {
- 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;
- }
- }
- // Consent form was closed.
- }
- @Override
- public void onConsentFormError(String errorDescription) {
- Log.d(TAG, "Requesting Consent: onConsentFormError. Error - " + errorDescription);
- // Consent form error.
- }
- })
- .withPersonalizedAdsOption()
- .withNonPersonalizedAdsOption()
- .withAdFreeOption()
- .build();
- form.load();
- }
- private void showPersonalizedAds() {
- AdView mAdView = findViewById(R.id.adView);
- AdRequest adRequest = new AdRequest.Builder()
- .addTestDevice("your device id from logcat") // Todo: Remove in Release build
- .build();
- mAdView.loadAd(adRequest);
- }
- private void showNonPersonalizedAds() {
- AdView mAdView = findViewById(R.id.adView);
- AdRequest adRequest = new AdRequest.Builder()
- .addTestDevice("your device id from logcat") // Todo: Remove in Release build
- .addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())
- .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");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement