如何确保在android中显示退出广告?

时间:2016-03-16 08:30:16

标签: android interstitial mopub

在我的Android应用中,我使用MoPub在应用退出时显示插页式广告。它工作正常,除非用户快速按两次后退按钮,应用程序退出时不显示任何插页式广告。我想确保展示插页式广告是否可用。

这是相关代码

// InterstitialAdListener methods
    @Override
    public void onInterstitialLoaded(MoPubInterstitial interstitial) {
        // This sample automatically shows the ad as soon as it's loaded, but
        // you can move this show call to a time more appropriate for your app.
        // if (interstitial.isReady()) {
        //    mMoPubInterstitial.show();
        // }
        // We're doing nothing here, we'll load the ad when exit
    }

    @Override
    public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
        Log.d("MoPub", "Interstitial load failed: " + errorCode);
    }

    @Override
    public void onInterstitialShown(MoPubInterstitial interstitial) {
    }

    @Override
    public void onInterstitialClicked(MoPubInterstitial interstitial) {
    }

    @Override
    public void onInterstitialDismissed(MoPubInterstitial interstitial) {
        finish(); //this will exit the program if interstitial is closed
    }

这是我的onBackPressed方法,我在那里展示广告

@Override
    public void onBackPressed() {
            if (mInterstitial.isReady()) {
                mInterstitial.show(); //this will show the interstitial if it's ready
            } else {
                super.onBackPressed(); //this will exit the program
            }
    }

1 个答案:

答案 0 :(得分:0)

您可以实现/覆盖onBackButtonPreesed方法。你可以使用一个计数器跟踪用户点击它的次数。因此,无论用户按下多少次或快速,您都可以完全控制它。所以,您点击后退按钮检查网络连接并展示广告并完成您的活动。