我对android的Interstitial广告有个问题。当应用程序启动时,广告显示,然后当用户移动到另一个活动并返回到主活动时,广告再次显示。如果广告在主活动中只显示一次,直到下一次重新启动,即使用户移动到另一个活动,我应该做什么呢?谢谢!这是我的密码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabpmnth_xm);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-xxxxxxxxxxxx8x/x2xxxxxxxx");
AdRequest aadRequest = new AdRequest.Builder()
.build();
// Begin loading your interstitial.
interstitial.loadAd(aadRequest);
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
}
});
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}发布于 2015-06-13 21:30:04
为应用程序创建一个应用程序类并执行以下操作
public class MyApplication extends Application{
@Override
public void onCreate(){
super.onCreate();
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("ShowInterstitial", TRUE).apply();
}
}然后在你的主要活动中
public void displayInterstitial() {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (interstitial.isLoaded() && sp.getBoolean("ShowInterstitial", true)){
interstitial.show();
PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("ShowInterstitial", false).apply();
}
}https://stackoverflow.com/questions/30823327
复制相似问题