在我的新应用中,我想使用插页式广告。可以通过增加变量重新启动应用程序。当此变量playcount
达到10且游戏结束时,将弹出广告并重置变量。它适用于我的模拟器,但在真实设备上,广告不会弹出,变量也不会重置。
public class GameScreen extends Activity {
public TextView tv;
InterstitialAd mInterstitialAd;
private AdView mAdView;
public int playcount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
TextView playctv = (TextView) findViewById(R.id.textView);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder()
.build();
mInterstitialAd.loadAd(adRequest);
SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
playcount = prefsplay.getInt("play_number", 0);
playctv.setText(String.valueOf(playcount));
}
public void restart(View view) {
Intent intent = new Intent(this, GameScreen.class);
startActivity(intent);
playcount++;
SharedPreferences prefsplay = this
.getSharedPreferences("myPrefsKey",
Context.MODE_PRIVATE);
prefsplay.edit().putInt("play_number", playcount)
.apply();
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
private void adView() {
mAdView = (AdView) findViewById(R.id.adView);
MobileAds.initialize(getApplicationContext(), getResources().getString(R.string.interstitial_full_screen));
final AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void gameover() {
mCountDownTimer.cancel();
imageButton1.setVisibility(View.VISIBLE);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if(playcount == 10) {
mInterstitialAd.show();
playcount = 0;
AdRequest adRequest = new AdRequest.Builder()
.build();
}
}
});}
}