插页式广告不重复

时间:2017-06-02 16:42:54

标签: timer admob interstitial

我在一段时间后因显示间隙广告而陷入困境。我的代码只显示一次(60秒后)。我希望每隔60秒显示一次插页式广告。我知道以这种方式实施广告并不是一个好主意,但我需要这个..我的代码如下:

package com.ronie.admobads;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends AppCompatActivity {
    InterstitialAd mInterstitialAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// Banner Ad
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

// Prepare the Interstitial Ad
        mInterstitialAd = new InterstitialAd(MainActivity.this);

// Insert the Ad Unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
        mInterstitialAd.loadAd(adRequest);


        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                        MainActivity.this.mInterstitialAd.show();
                    }
                }, 60000);
            }
        });
    }
}

@Abhishek,我尝试过你的方式,但非页内广告并没有显示出来。我只能看到横幅广告。请问我在代码中的任何地方检查我的错误吗? 完整代码如下:

package com.ronie.admobads;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    InterstitialAd mInterstitialAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// Banner Ad
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

// Prepare the Interstitial Ad
        mInterstitialAd = new InterstitialAd(MainActivity.this);

// Insert the Ad Unit ID
        mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));
        mInterstitialAd.loadAd(adRequest);


        mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // don't show Ad here
            }

            @Override
            public void onAdClosed() {
                createRequest();   //load request whenever ad closed by user
            }
        });

        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() {
                if (mInterstitialAd.isLoaded())
                    mInterstitialAd.show();
                else
                    mInterstitialAd.show();
                createRequest();
            }
        }, 1,1, TimeUnit.MINUTES);
    }

        public void createRequest(){

            AdRequest adRequest = new AdRequest.Builder().build();
            mInterstitialAd.loadAd(adRequest);
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式安排活动:

createRequest();

mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                // don't show Ad here
            }

            @Override
            public void onAdClosed() {
                createRequest();   //load request whenever ad closed by user
            }
        });

ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
                public void run() {
                    if(mInterstitialAd.isLoaded())
                        mInterstitialAd.show();
                    else
                        createRequest();
                }
            }, 1, 1, TimeUnit.MINUTES);

createRequest方法

public void createRequest(){

    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}

修改

我的错误,我道歉,线程因java.lang.IllegalStateException而死,需要调用主UI线程。

scheduler.scheduleAtFixedRate(new Runnable() {
                public void run() {

                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              if (mInterstitialAd.isLoaded())
                                  mInterstitialAd.show();
                              else
                                  createRequest();
                          }
                      });
                }
            }, 1, 1, TimeUnit.MINUTES);