我的问题是,当我将一个活动切换到另一个活动然后admob需要时间加载,所以我希望它应该只在每个屏幕上加载一次,所以让我知道如果有可能然后如何在数据库中存储广告的可能性或分享偏好或其他方式,请帮助我 我的代码是
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
layout = (RelativeLayout) findViewById(R.id.addd);
layout.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
答案 0 :(得分:0)
我建议使用布尔值:
public class AdsClass extends Activity {
private Boolean adLoaded;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// When activity is created by first time adLoaded = false
// After activity is created, onResume() method loads
adLoaded = false;
}
@Override
// onResume() is loaded after onCreate -> onStart(); and when activity is resumed
public void onResume() {
super.onResume();
// On creation, ad is loaded and while app keeps running (even in background) ad will not load anymore
// until you stop the app
if (adLoaded) {
Log.e("Ad", "Skipping");
}else {
loadAdvertisement();
}
}
public void loadAdvertisement() {
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
layout = (RelativeLayout) findViewById(R.id.addd);
layout.setGravity(Gravity.CENTER_VERTICAL);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder().build();
adLoaded = true;
}
}
如果您不想加载广告甚至更改活动,则需要在每次活动更改中使用Intent Extra:
Send Extra
Intent intent = new Intent(this, NextActivity.class);
intent.putExtras("adLoadedInAdsClass", adLoaded);
Receive Extra in other activity
@Override
protected void onCreate(Bundle savedInstanceState) {
Boolean adLoaded = getIntent().getExtras().getBoolean("adLoadedInAdsClass");
}