gpt4 book ai didi

android - 单击菜单按钮后如何在启动新 Activity 之前显示插页式广告?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:12 25 4
gpt4 key购买 nike

我使用选项菜单按钮转到第二个 Activity 。当用户在启动第二个 Activity 后单击该菜单按钮插页式广告时。但是我想在启动第二个 Activity 之前显示插页式广告,当用户点击插页式广告的关闭按钮时,第二个 Activity 应该启动。

我正在使用下面的代码来显示插页式广告。

case R.id.button_id:
startActivity(new Intent(this, secondactivity.class ));

interstitial = new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId(getString(R.string.admob_interstetial_ad));
AdRequest adRequest9 = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)


.build();
interstitial.loadAd(adRequest9);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});


return true;

最佳答案

@user8240773 建议的答案是正确的,但有一种更有效的方法来处理您的问题。这是我的代码:

// Has the interstitial loaded successfully?
// If it has loaded, perform these actions
if(mInterstitialAd.isLoaded()) {
// Step 1: Display the interstitial
mInterstitialAd.show();
// Step 2: Attach an AdListener
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Step 2.1: Load another ad
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_EMULATOR_ID)
.build();
mInterstitialAd.loadAd(adRequest);

// Step 2.2: Start the new activity
startActivity(new Intent(Activity1.this, Activity2.class));
}
});
}
// If it has not loaded due to any reason simply load the next activity
else {
startActivity(new Intent(Activity1.this, Activity2.class));
}

这样一来,您也不必担心由于没有互联网连接或其他原因导致广告无法加载。此代码将按照您描述问题的方式处理所有内容。

关于android - 单击菜单按钮后如何在启动新 Activity 之前显示插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44859920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com