Timer 向pdf库添加间隙
我试着用我找到的pdf库创建一本书,我创建了我的书一切正常,我只想添加中间admob并每隔30秒显示一次,我尝试使用handler runnable,但它仍然只显示一次Timer 向pdf库添加间隙,timer,admob,handler,interstitial,Timer,Admob,Handler,Interstitial,我试着用我找到的pdf库创建一本书,我创建了我的书一切正常,我只想添加中间admob并每隔30秒显示一次,我尝试使用handler runnable,但它仍然只显示一次 mInterstitialAd = new InterstitialAd(this); mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); mInterstitialAd.loadAd(new AdRequest.Builder().bui
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);
}