1

I'm currently developing an app with AdMob's interstitial ads. I'm trying to destroy the ad, but it doesn't appear to work.

I've tried doing that with mInterstitialAd.destroy();

It doesn't work with the error Cannot resolve method 'destroy'.

an image of a code listing

Shog9
  • 156,901
  • 35
  • 231
  • 235
FearKill
  • 23
  • 5
  • you cannot destroy Interstitial Ad & it also doesn't have any destroy(); method!, what are you trying to achieve? – Darshan Nov 17 '18 at 15:26
  • Can you please post your full code? – Ishaan Javali Nov 17 '18 at 15:43
  • @DarShan Hey, thanks for the comment. I'm trying to close the ad automatically after its showed. If there's another way, please let me know. :) – FearKill Nov 17 '18 at 16:29
  • @Ishaan sure, its full of trash but I'm very new so there it is: – FearKill Nov 17 '18 at 16:30
  • @Ishaan oh, its too long, so I'll make its short: private void showInterstitial() { // Show the ad if it's ready. Otherwise toast and reload the ad. if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { mInterstitialAd.show(); mInterstitialAd.destroy(); //something } I'm trying to automatically close the ad after its shown. – FearKill Nov 17 '18 at 16:31
  • I found [this similar question on Stack Overflow](https://stackoverflow.com/questions/22145885/programmatically-close-an-interstitial-ad) about how to close an interstitial ad. Basically, the answer given for that question was to programatically call the back button like this: `this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));` I don't know if this will work, but you should try it out and check. Also check the [link](https://stackoverflow.com/questions/22145885/programmatically-close-an-interstitial-ad) to the question I gave you. – Ishaan Javali Nov 17 '18 at 17:10
  • @Ishaan Hey man, thanks for the comment, but I wouldn't ask this question if I found it on another question lol. I've tried it, and it doesn't work, plus I want it to close automatically, without pressing a button. – FearKill Nov 17 '18 at 17:14
  • What action are you expecting or what do you want to happen? When should the ad get destroyed? – Ishaan Javali Nov 17 '18 at 17:19
  • @Ishaan As I mentioned, I'm very new, so I just want to see how things work and stuff. – FearKill Nov 17 '18 at 17:25
  • **No.** What I mean is what is the **expected output?** When should the **ad close?** – Ishaan Javali Nov 17 '18 at 17:43
  • @Ishaan One millisecond later. – FearKill Nov 17 '18 at 17:50
  • So you want the ad to open and then close a millisecond later? Try using a `Handler` and `Runnable` to wait for 1 millisecond and then trigger the back button. – Ishaan Javali Nov 17 '18 at 17:59

1 Answers1

1

To wait a millisecond before closing the ad, try this:

Runnable r = new Runnable() {
    @Override
    public void run() {
        MainActivity mainActivity = new MainActivity();
        mainActivity.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
    }
};
Handler h = new Handler();
h.postDelayed(r, 1);

The runnable is executed, or run, after 1 millisecond. When it is run, it programatically triggers the back button to close the ad.

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
  • Hey, excuse me, can you please let me know where do I put it? As I mentioned I'm very new, so sorry for disturbing that much. – FearKill Nov 17 '18 at 19:25
  • This goes after the part where you create your ad. So I don't know where you are creating your ad, but I am guessing that it is in your `MainActivity`. If not, change the `MainActivity` from my code to the name of your activity. Once again, this part should go after you create the ad. **I'm just advising, since the ad closes after 1 millisecond, you probably won't be able to see the ad. You may want to increase the `1` to `1000`, that way it is there for a second before closing.** Hope this helps! – Ishaan Javali Nov 17 '18 at 19:35
  • Did the code work for you? If not just post your problem below. If it did, please mark it as accepted so that others can know to refer to it for help. Thanks! – Ishaan Javali Nov 17 '18 at 21:32
  • Hey, I've posted a image for the code, can you please check it out and tell me where should I paste it? Thank you! – FearKill Nov 18 '18 at 18:05
  • Ok. So based on the picture, I am assuming that in `onAdLoaded()`, that is where you are creating the ad because you are then saying `mInterstitialAd.show()`. So after the ad is shown, that is where you should place the code I gave you that closes the ad. I don't know what you are using `showInterstitial()` or `loadInterstitial()` for, but if that is where you create the ad, then paste my code there. – Ishaan Javali Nov 18 '18 at 18:47