我有一个应用程序,有两个活动(主要活动和学习活动)。第一个活动包含一个按钮。点击它会将用户导航到另一个屏幕。在导航到另一个屏幕之前,会显示中间广告。问题是当我关闭中间主活动时,它会在导航到学习活动之前出现1-2秒。如何修复?
这是我的主要活动课:
public class MainActivity extends AppCompatActivity {
AdsManager adsManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadAds();
}
void loadAds() {
adsManager = new AdsManager(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.learnButton:
Intent intent = new Intent(this, LearningActivity.class);
if (adsManager != null) {
adsManager.showAd(intent);
} else {
startActivity(intent);
}
break;
}
下面是我的adsmanager类:
class AdsManager {
private InterstitialAd mInterstitialAd;
private Context context;
AdsManager(Context context) {
this.context = context;
MobileAds.initialize(context, context.getString(R.string.adMobAppId));
mInterstitialAd = new InterstitialAd(context);
mInterstitialAd.setAdUnitId(context.getString(R.string.adMobUnitId));
Bundle extras = new Bundle();
extras.putBoolean("is_designed_for_families", true);
AdRequest.Builder builder = new AdRequest.Builder()
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
.tagForChildDirectedTreatment(true);
mInterstitialAd.loadAd(builder.build());
}
void showAd(Intent intent) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
context.startActivity(intent);
}
});
}
else {
context.startActivity(intent);
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!