我需要在我的应用中每隔一定时间展示插页式广告。我已经设置了代码,它可以正常工作,但问题是,即使应用程序关闭,插页式广告仍然会出现。
当应用程序关闭时,我该如何停止插页式广告的显示?
谢谢。
当应用程序关闭时,我该如何停止插页式广告的显示?
谢谢。
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prepareAd();
ScheduledExecutorService scheduler =
Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
Log.i("hello", "world");
runOnUiThread(new Runnable() {
public void run() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG"," Interstitial not loaded");
}
prepareAd();
}
});
}
}, 10, 10, TimeUnit.SECONDS);
}
public void prepareAd() {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
}