App occasionally freezes at start when trying to show Ad

44 views
Skip to first unread message

Rik Norakomi

unread,
Nov 17, 2014, 5:38:45 AM11/17/14
to google-adm...@googlegroups.com
Hi all,

I created a gamethat startup with showing my Logo splashscreen and then goes to a Title screen.
Before the screenswitch takes places the Apps checks to see if the device is connected to the Wifi and if so shows an interstitial ad before switching to the title screen.

Occasionally though the app freezes up at this point where it should show an interstitial ad and I can't figure out how to solve it.

Maybe you could help  me?

Down below is the code for my AndroidLauncher.java


private static final String BANNER_AD_UNIT_ID = "ca-app-pub-5519384153835422/6018415393";
private static final String INTERSTITIAL_AD_UNIT_ID = "ca-app-pub-5519384153835422/1588215795";
private static final String NORAKMI_BLOG_URL = "http://norakomi.blogspot.nl/";
private static final String DEBUG_TAG = "NetworkStatusExample";
// private static final String TRACKING_ID = "UA-56148307-4";

AdView bannerAd;
InterstitialAd interstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new PacManbowGame(this), config);
setupAds();
RelativeLayout layout = new RelativeLayout(this);
layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(bannerAd, params);
setContentView(layout);
}

public void setupAds() {
bannerAd = new AdView(this);
bannerAd.setVisibility(View.INVISIBLE);
bannerAd.setBackgroundColor(0xff000000); // black
bannerAd.setAdUnitId(BANNER_AD_UNIT_ID);
bannerAd.setAdSize(AdSize.SMART_BANNER);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(INTERSTITIAL_AD_UNIT_ID);

if (isNetworkConnected()) {
AdRequest.Builder builder = new AdRequest.Builder();
//builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
AdRequest ad = builder.build();
interstitialAd.loadAd(ad);
}
}

@Override
public void showInterstitialAd(final Runnable then) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (then != null) {
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
Gdx.app.postRunnable(then);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
interstitialAd.loadAd(ad);
}
});
}
interstitialAd.show();
}
});
}

@Override
public void showBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.VISIBLE);
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest ad = builder.build();
bannerAd.loadAd(ad);
}
});
}

@Override
public void hideBannerAd() {
runOnUiThread(new Runnable() {
@Override
public void run() {
bannerAd.setVisibility(View.INVISIBLE);
}
});
}

@Override
public void onBackPressed() {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

Button b1 = new Button(this);
b1.setText("Quit");
b1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
if (isNetworkConnected()) {
showInterstitialAd(new Runnable() {
public void run() {
System.exit(0);
}
});
} else {
System.exit(0);
}
}
});
ll.addView(b1);

Button b2 = new Button(this);
b2.setText("NORAKOMI");
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri
.parse(NORAKMI_BLOG_URL)));
dialog.dismiss();
}
});
ll.addView(b2);

dialog.setContentView(ll);
dialog.show();
}

@Override
public boolean isNetworkConnected() {

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
boolean isWifiConn = ni.isConnected();
// ni = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//boolean isMobileConn = ni.isConnected();
//System.out.println(DEBUG_TAG + "Wifi connected: " + isWifiConn);
//Log.d(DEBUG_TAG, "Mobile connected: " + isMobileConn);

//System.out.println(DEBUG_TAG + " Mobile || Wifi connected: " + (isWifiConn ));

return (ni != null && isWifiConn );

/*
if (ni == null) {
return false;
} else if (isMobileConn == true && isWifiConn == true) {
return true;
} else {
return false;
}
*/
}

}

In the render method when I want to switch screen the code is called through an interface (adsController):
if (switchScreen) {

if (game.getAdsController().isNetworkConnected() == true) {

game.getAdsController().showInterstitialAd(new Runnable() {
@Override
public void run() {

game.setScreen(game.titleScreen);
}
});

} else {
game.setScreen(game.titleScreen);
}
}




Eric Leichtenschlag (Mobile Ads SDK Team)

unread,
Nov 19, 2014, 8:12:17 PM11/19/14
to google-adm...@googlegroups.com
Hello,

I can't see anything obvious from the code, but I'd recommend removing components like checking for connectivity and all the threading to reduce the scope of the issue.

I also don't see you calling super.onBackPressed() anywhere in your onBackPressed method. It may not have anything to do with the freezing, but it's an android best practice to do this after the user confirms they want to quit.

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages