Android:当用户点击后退按钮退出应用程序时显示mopub中间广告

Android:当用户点击后退按钮退出应用程序时显示mopub中间广告,android,exit,mopub,interstitial,Android,Exit,Mopub,Interstitial,我正在开发一个android应用程序,当用户按下后退按钮退出应用程序时,我想显示一个mopub插入式全屏广告 我尝试过创建间隙广告并用onDestroy方法显示它。诸如此类: @Override public void onDestroy(){ this.interstitial = new MoPubInterstitial(this, MY_INTERSTITIAL_AD_UNIT_ID_HERE); this.interstitial.setInterstitialAdLi

我正在开发一个android应用程序,当用户按下后退按钮退出应用程序时,我想显示一个mopub插入式全屏广告

我尝试过创建间隙广告并用onDestroy方法显示它。诸如此类:

@Override
public void onDestroy(){
    this.interstitial = new MoPubInterstitial(this, MY_INTERSTITIAL_AD_UNIT_ID_HERE);
    this.interstitial.setInterstitialAdListener(this);
    this.interstitial.load();

    super.onDestroy();
}

// InterstitialAdListener method
@Override
public void onInterstitialLoaded(MoPubInterstitial interstitial) {
    if (interstitial.isReady()) {
        mInterstitial.show();
    } else {
        // Other code
    }
}
但是,我没有在任何地方销毁Interstitual(minterstitual.destroy();),因为我不知道在哪里可以这样做,因此我得到以下错误:

Activity com.myActivity has leaked IntentReceiver com.mopub.mobileads.MoPubView$1@41baffc0 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.myActivity has leaked IntentReceiver com.mopub.mobileads.MoPubView$1@41baffc0 that was originally registered here. Are you missing a call to unregisterReceiver()?
虽然我遇到了这个错误,但是显示了add(我已经在许多设备上测试过),并且它似乎在除索尼以外的所有设备上都运行良好

如何改进此代码以在退出时显示间隙


谢谢

在ondestroy时加载Interstitual。当你的活动被破坏后,你是泄漏。在ondestroy中调用interstitual.destroy()


您可能想做的是处理onbackpressed。最后,我发现了一种叫做positial的新广告技术,它让我在用户退出应用程序时以自然的方式显示mopub全屏广告。您可以在此处看到更多信息:

在onDestroy()中确保销毁mopubview

    if (mMoPubView!=null) {
        mMoPubView.destroy();
    }

您可以尝试重载onBackPressed方法。 这样,onDestroy仍然可以用于清理间隙

在你的活动中

@Override
public void onBackPressed(){
    if (interstitial.isReady()) {
        interstitial.show(); //this will show the interstitial if it's ready
    } else {
        super.onBackPressed(); //this will exit the program
    } 
}
然后在侦听器中调用finish(),当返回间隙时(当失败或关闭时)

当然,你仍然需要按照正常方式调用销毁

@Override
protected void onDestroy() {
    interstitial.destroy();
    super.onDestroy();
}

以前的帖子,但我现在正在寻找这个问题,并找到了这个帖子

现在,如果您遇到关于“活动已泄漏窗口”的错误,请记住将finish()放在onAdClosed上,而不放在其他位置

mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                finish();
                requestNewInterstitial();

            }
        });

如果您希望关闭活动并显示间隙。

100%工作解决方案,我们需要为间隙和横幅广告调用销毁函数

 @Override
    protected void onDestroy() {

        if(mopubInterstitial != null){
            mopubInterstitial.destroy();
        }
        if (mopubBannerPubView != null){
            mopubBannerPubView.destroy();
        }
        super.onDestroy();
    }

我已经用我使用的解决方案回答了这个问题,但也许你的回答也可以有效。非常感谢。嘿!链接断了。
 @Override
    protected void onDestroy() {

        if(mopubInterstitial != null){
            mopubInterstitial.destroy();
        }
        if (mopubBannerPubView != null){
            mopubBannerPubView.destroy();
        }
        super.onDestroy();
    }