I have a big problem in my game with AdMob Interstitials. The ad is shown when the timer is finished or when the player exit from the round. So at finish of the activity. When there isn't connection, it's ok. The change of activity is fast. When there is a god and fast connection the same. The problem is when there is a worth connection and when the player finish the round, there is a long time (sometimes the ad is never shown) where the activity don't change, so the player can continue to play even tough the time is over. This is my code:
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
}
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
Intent data = new Intent();
data.putExtra("team", team);
myDB.close();
data.putExtra("A", A);
data.putExtra("B", B);
// data.putExtra("contaDb", this.contDb);
setResult(1, data);
finish();
}
public void onLeaveApplication1(Ad arg0) {
// TODO Auto-generated method stub
}
public void onPresentScreen1(Ad arg0) {
// TODO Auto-generated method stub
}
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
if(ad.isReady())
interstitial.show();
}
Intent data = new Intent();
data.putExtra("team", team);
myDB.close();
data.putExtra("A", A);
data.putExtra("B", B);
// data.putExtra("contaDb", this.contDb);
setResult(1, data);
finish();
}
public void onDismissScreen1(Ad arg0) {
// TODO Auto-generated method stub
}
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
And:
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null)
return false;
return ni.isConnected();
}
public void visualizzaRisultatoActivity() {
if (isOnline()) {
interstitial = new InterstitialAd(this, "0976ebb3525a494a");
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
} else {
Intent data = new Intent();
data.putExtra("team", team);
myDB.close();
data.putExtra("A", A);
data.putExtra("B", B);
// data.putExtra("contaDb", this.contDb);
setResult(1, data);
finish();
}
}
VisualizzaRisultatoActivity is called when the time is finished or the player cancel the round. Thanks in advance
The best practice is to preload your interstitial during the game, and then polling if it's ready at the end of the game:
if(interstitial.isReady()) {
interstitial.show();
}
The isReady
flag gets set when your interstitial calls onReceiveAd
, but you can wait to show it until the end of the game. Note that you should probably request/show your interstitial within the game activity, instead of the your VisualizzaRisultatoActivity. The interstitial is expected to be shown with the same activity you passed into its constructor.