Skip to content

Instantly share code, notes, and snippets.

@ibrahimsn98
Forked from ameerhamza6733/AdmobConsentSimple
Created June 18, 2018 16:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ibrahimsn98/ef125685cb3dae5ed00cf0fb3b8b7ee5 to your computer and use it in GitHub Desktop.
Save ibrahimsn98/ef125685cb3dae5ed00cf0fb3b8b7ee5 to your computer and use it in GitHub Desktop.
Simple for Consent sdk android admob
/*https://www.youtube.com/watch?v=_JOapnq8hrs&t=852s
video tutorial for consent sdk android
*/
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.
/*
watch this video how to create privacy policy in mint
https://www.youtube.com/watch?v=lSWSxyzwV-g&t=140s
*/
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();
}
/*
want test your app watch this video https://youtu.be/_JOapnq8hrs?t=654
*/
private void showPersonalizedAds() {
/* this line code save consent status if you want to show your ads in next activty just get getConsentStatus and load ads accouding to status e.g.
MainActivty2 ConsentStatus consentStatus =ConsentInformation.getInstance(this).getConsentStatus();
if (consentStatus.toString().equals("NON_PERSONALIZED")) loadNonPersonlizedAds(); i hateeeeeeeeeet This new policy
*/
ConsentInformation.getInstance(this).setConsentStatus(ConsentStatus.PERSONALIZED);
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);
// if you want to show interstital
/*
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("Add_unit");
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(DEVICE_TEST_ID).build());
*/
}
private void showNonPersonalizedAds() {
ConsentInformation.getInstance(this).setConsentStatus(ConsentStatus.NON_PERSONALIZED);
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);
/* if you want show interstitial ad also
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("Add_unit");
mInterstitialAd.loadAd(new AdRequest.Builder().addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle()).addTestDevice(DEVICE_TEST_ID).build());
*/
}
public Bundle getNonPersonalizedAdsBundle() {
Bundle extras = new Bundle();
extras.putString("npa", "1");
return extras;
}
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");
}
}
}
@lobanovSpace
Copy link

Hello. How to write the code for the MainActivity2 correctly?
ConsentStatus consentStatus = ConsentInformation.getInstance(this).getConsentStatus(); if (consentStatus.toString().equals("NON_PERSONALIZED")){ loadNonPersonalizedAds(); }
What is missing in this code to make it work successfully?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment