控制插页式广告的外观

Control the appearance of an interstitial ad

我有一个插页式广告的问题,它不会出现在游戏中间,但会在玩家离开并重新进入时出现。有没有显示游戏中间的解决方案。

private ZenGL zengl;
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
private static final String IAD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
protected AdView adView;
private InterstitialAd mInterstitialAd;
private boolean needAds = false;

public void onAdLoaded() {
    mInterstitialAd.show();
};

@Override
public void onResume() {
    if ( zengl != null )
        zengl.onResume();

    if (adView != null)
        adView.pause();

    if (mInterstitialAd.isLoaded() & (needAds==true)) {
        mInterstitialAd.show();
        needAds=false;
    }  
    super.onResume();
}

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
    //  .addTestDevice("")
    .build();

    mInterstitialAd.loadAd(adRequest);
    needAds = true;
}

如何编辑这些代码以展示期中游戏广告?

参考官方文档放置广告。 https://developers.google.com/admob/android/interstitial

如果您想在游戏进行到一半时显示广告,一种简单的方法是创建一个单独的线程,它在您的游戏开始时启动,并在每个固定的时间间隔触发事件以显示广告来自线程。

private void showAd(){
    new AsyncTask<Void, Void, Void>(){

        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    }.execute();
}

此函数将在 3 秒后显示广告;)