0

i want load interstitial ad in activity and show it in another activity . i found this topic and try to do the steps in the first answer but there are a lot errors .

How To preload admob interstitial ad and send to another android activity using intent

That what i did :

  • I created a public class and named it "AdManager" and put this code in it:

    `package com.website.test;
    import com.google.android.gms.ads.InterstitialAd;
    
    

    public class AdManager {

    // Static fields are shared between all instances. static InterstitialAd ad;

    public AdManager() { createAd(); }

    public void createAd() { // Create an ad. interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId("");

    AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice(TEST_DEVICE_ID).build(); // Load the interstitial ad. interstitialAd.loadAd(adRequest); } public InterstitialAd getAd() { return ad;}}
  • In create event of class A i put this :

AdManager adManager = new AdManager(); adManager.createAd();

  • In create event of the activity which i want show interstitial ad i put this :

InterstitialAd ad = admanager.getAd(); if (ad.isLoaded) { ad.show(); }

but there are some errors as it shown in the pics i attached , please tell me what is wrong ?

screenshot1

screenshot2

Community
  • 1
  • 1
khaled
  • 41
  • 2
  • 8

2 Answers2

2
class AdManager {
// Static fields are shared between all instances.
private static InterstitialAd interstitialAd;

private static boolean isInterAdsShowed = false;
private Activity activity;
private String AD_UNIT_ID;

AdManager(Activity activity, String AD_UNIT_ID) {

    this.activity = activity;
    this.AD_UNIT_ID = AD_UNIT_ID;
    createAd();
}

void createAd() {
    // Create an ad.
    interstitialAd = new InterstitialAd(activity);
    interstitialAd.setAdUnitId(AD_UNIT_ID);

    AdRequest adRequest = new AdRequest.Builder()
            //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            //.addTestDevice(TEST_DEVICE_ID)
            .build();

    // Load the interstitial ad.
    interstitialAd.loadAd(adRequest);
}

static InterstitialAd getAd() {
    if(interstitialAd != null && interstitialAd.isLoaded() && !isInterAdsShowed) {
        isInterAdsShowed = true;
        return interstitialAd;
    }
    else return null;
}

}

..... Activity A

  AdManager adManager = new AdManager(this,"your ads id");
    adManager.createAd();

.... Activity B

 InterstitialAd ad = AdManager.getAd();
    if (ad != null) {
        ad.show();
    }
mouness2020
  • 241
  • 3
  • 8
1

DO NOT DO THIS You are almost certainly going to leak memory and eventually crash your app.

The errors in your images are basic Java syntax issues.

Image 1

  1. interstialAd variable is not declared
  2. AdRequest has not been imported
  3. AD_UNIT_ID has not been declared

Image 2

  1. adManager has not been declared.
William
  • 20,150
  • 8
  • 49
  • 91