How to preload an AdMob ad and send another Android activity using intent

I need help regarding an AdMob ad.

I want to preload an interstitial in one step. it is straight.

// Create an ad.
        interstitialAd = new InterstitialAd(this);
        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);

Now I want to send an interstitial to another action using the intent. I do not know how to send it using

intent.putExtra("myAd", interstitialAd);

Thanks in advance.

+4
source share
5 answers

Intermediate advertising is not intended and is not intended to be distributed with the help of additional materials of intent.

It's better

  • recreate & reload ad in next action
  • , , A B

2- ():

public class AdManager {
    // Static fields are shared between all instances.
    static InterstitialAd ad;
    private Context ctx;

    public AdManager(Context ctx) {
        this.ctx = ctx;
        createAd();
    }

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

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

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

    public InterstitialAd getAd() {
        return ad;
    }
}

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

B

AdManager adManager = new AdManager();
InterstitialAd ad = adManager.getAd();
if (ad.isLoaded()) {
    ad.show();
}
+13

.

/**
* Created by Kirk on 10/26/2017.
*/

public class AdManager {

private static AdManager singleton;

public AdManager() {
}

/***
 * returns an instance of this class. if singleton is null create an instance
 * else return  the current instance
 * @return
 */
public static AdManager getInstance() {
    if (singleton == null) {
        singleton = new AdManager();
    }

    return singleton;
}

/***
 * Create an interstitial ad
 * @param context
 */
public static void createAd(Context context) {
    interstitialAd = new InterstitialAd(context);
    interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    interstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}

/***
 * get an interstitial Ad
 * @return
 */
public static InterstitialAd getAd() {
    return interstitialAd;
}

}

activityA

    AdManager adManager = AdManager.getInstance();
    adManager.createAd(MainActivity.this);

B

    AdManager adManager = AdManager.getInstance();
    InterstitialAd ad =  adManager.getAd();

    if (ad.isLoaded()) {
        ad.show();
    }
+5

InterstitialAd Context, ( ) onCreate Application, .

, .

AdManager ( ), .

+2

ThisActivity PreLoader :

int preLoaderId = PreLoader.preLoad(new Loader());
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("preLoaderId", preLoaderId);
startActivity(intent);

//DataLoader, pre-load ad object
class Loader implements DataLoader<InterstitialAd> {
    @Override
    public InterstitialAd loadData() {

        // Create an ad object.
        InterstitialAd interstitialAd = new InterstitialAd(ThisActivity.this);
        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);
        return interstitialAd;
    }
}

OtherActivity, :

PreLoader.listenData(preLoaderId, new Listener());

//after ad load completed,DataListener.onDataArrived(...) will be called to process data
class Listener implements DataListener<InterstitialAd> {
    @Override
    public void onDataArrived(InterstitialAd ad) {
        //do sth with ad object
        //destroy this PreLoader by id if you don`t need it anymore
        PreLoader.destroy(preLoaderId);
    }
}

, .

0

, , !

!

    public AdManager(Context ctx) {
        this.ctx = ctx;
        //createAd(); //Remove this line! 
    }

Finally, the AdManager class should look like this:

public class AdManager {
    // Static fields are shared between all instances.
    static InterstitialAd ad;
    private Context ctx;

    public AdManager(Context ctx) {
        this.ctx = ctx;
       // createAd();
    }

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

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

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

    public InterstitialAd getAd() {
        return ad;
    }
}

Using

Lesson A

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

Activity B

AdManager adManager = new AdManager();
InterstitialAd ad = adManager.getAd();
if (ad.isLoaded()) {
    ad.show();
}
0
source