将插页式广告添加到 pdf 库

Add interstitial to pdf library

我尝试用找到的 pdf 库创建一本书 this library,我创建了我的书,一切正常,我只想添加插页式 admob 并每 30 秒显示一次,我尝试使用可运行的处理程序,但它是仍然只显示一次。

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
handler = new Handler();

final Runnable r = new Runnable() {
    public void run() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
        handler.postDelayed(this, 1000);
    }
};
handler.postDelayed(r, 1000);

您可以使用 ScheduledExecutorService,并安排定期操作。

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

        @Override
        public void onAdClosed() {
            createRequest();   //load request whenever ad closed by user
        }
    });
createRequest();
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() {

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

createRequest()方法

public void createRequest(){

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