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

后端 未结 5 1290
醉话见心
醉话见心 2020-12-10 08:12

I need some help regarding AdMob interstitial ad.

I want to preload the interstitial ad in one activity. this is straight forward.

// Create an ad.
          


        
5条回答
  •  轮回少年
    2020-12-10 08:31

    In ThisActivity, you can pre-load ad object with PreLoader asynchronously:

    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 {
        @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;
        }
    }
    

    in OtherActivity, you can get ad object by:

    PreLoader.listenData(preLoaderId, new Listener());
    
    //after ad load completed,DataListener.onDataArrived(...) will be called to process data
    class Listener implements DataListener {
        @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);
        }
    }
    

    wish this is helpful.

提交回复
热议问题