Search code examples
androidadmobinterstitialsetcontentview

Interstitial Ad with setContentView


I'm trying to add an interstitial to my app that uses setContentView. I've already got a "listener" implemented that properly calls whenever an ad is needed. You can abbreviate your answer by calling this function "Listener"{...} and assume that everything within the brackets will occur whenever the app signals for an ad. The following answer has a good method for Banner ads.

Implementing Admob banner when setContentView() is used for the Surfaceview

My question is, in this scenario, how should I implement an interstitial?

My code is basically the answer from the link, plus this:

...    
        setContentView(layout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        listener= new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent event) {
                if (event.getPropertyName()=="sharing1") {
                    caring();
                    game.dead=false;
                    game.sharing1=false;
                }
                if(event.getPropertyName()=="dead")
                {
                //Make an interstitial ad here
                }
            }
        };

Solution

  • Not sure what you're asking but unlike banner ads, an interstitial ad is a pop up which displays independently of the underlying activity's content view.

    You can call your function after calling #setContentView(). Perhaps you should post some code so we know what specific problems you're facing

    UPDATE: You can preload and show it only when needed, in this case when the player "dies". This would reduce latency. After each display of an ad, you must reload the ad so they get shown upon subsequent calls. Also, use a handler if your listener would be invoked in a non-UI thread. Here's a solution:

    ...   
    
    
    
      InterstitialAd interstitial; 
            setContentView(layout);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            InterstitialAd interstitial = new InterstitialAd(this);
            interstitial.setAdUnitId("your-ad-unit");
            AdRequest adRequest = new AdRequest.Builder().build();
            // Preload your interstitial to avoid load time delays 
            interstitial.loadAd(adRequest);
            listener= new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent event) {
                    if (event.getPropertyName()=="sharing1") {
                        caring();
                        game.dead=false;
                        game.sharing1=false;
                    }
                    if(event.getPropertyName()=="dead")
                    {
                    //Make an interstitial ad here
            runOnUiThread(new Runnable() {
            @Override
            public void run() {
            if (interstitial != null && interstitial.isLoaded()){
                 interstitial.show();
                 //reload ad 
                 interstitial.loadAd(new AdRequest.Builder().build());             
                 }  
            }
        });
                    }
                }
            };