• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How can i load interstitial ad in activity and show is in another activity?

khaled2015

Lurker
Nov 30, 2015
2
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;



    publicclassAdManager{


    // Static fields are shared between all instances.
    staticInterstitialAd ad;


    publicAdManager(){
    createAd();
    }


    publicvoid createAd(){
    // Create an ad.
    interstitialAd =newInterstitialAd(this);
    interstitialAd.setAdUnitId("");


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

    // Load the interstitial ad.
    interstitialAd.loadAd(adRequest);
    }
    publicInterstitialAd 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 ?

mY4hL.jpg

WGBcz.jpg
 
Looks like you haven't declared variable interstitialAd.
You know if you hover over those red 'x' icons, it will tell you the error?

With respect, if you're getting stuck on problems like this, you're going to have a very hard time writing an app. I would recommend you get hold of a beginner's book on Java, or look at some web tutorials. You cannot just cut and paste code fragments without any understanding of what you're doing.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones