AdMob with PhoneGap 3.0 on resume displays blank p

2019-08-27 11:01发布

问题:

I've made an app using PhoneGap 3.0, which includes the Google AdMob and Facebook SDKs.

Sometimes, after the app is suspended (by pressing "home" for example), when it's launched again the browser viewport is blank (the AdMob ad at the bottom of the screen is still displayed). As soon as you touch inside the web view it renders the page correctly. Around 5 pixels at the bottom of the web view remain visible at all times.

I tried to get a screenshot to include here, but taking a screenshot also causes the page to be rendered correctly!

It sounds like an internal issue with Android/HTC Sense/drivers etc. The problem occurs when I test the app in Android 4.3 on an HTC One. I've also tried it on 2.3 and I can't reproduce the issue.

There isn't anything useful in the LogCat messages - the only event that might be related is MainScreen - Summary updated, which occurs when I touch the web view to force it to render the contents.

My next steps are to create a blank PhoneGap app to see if I can reproduce it, adding functionality (Facebook, AdMob) a bit at a time until it happens again to try and pinpoint the cause.

Does anyone have any experience of a similar issue, or could you suggest anything I can try?

UPDATE

If I remove the code the loads the AdMob ad, the problem goes away. This is definitely caused by AdMob. This is the code I'm using to load the Ad:

    mHandler.postDelayed(new Runnable() {
        public void run() {
            loadAd();
        }
    }, 2000); 


    private void loadAd() {
        adView = new AdView(this, AdSize.BANNER, "my-id");
        LinearLayout layout = super.root;
        layout.addView(adView);
        layout.setHorizontalGravity(android.view.Gravity.CENTER_HORIZONTAL);
        adView.loadAd(new AdRequest());
    }

回答1:

To work around the problem I remove the AdView when the app is paused, and create it again when it's resumed.

@Override
protected void onPause() {
  super.onPause();

  if (adView != null) {
    LinearLayout layout = super.root;
    layout.removeView(adView);
  }
}

This works fine so will do for now. It also prevents the AdView from continuing to load ads in the background so that's a bonus.

If anyone has any better answers feel free to let me know!



回答2:

You need override to response to onResume event.

@Override
public void onResume(boolean multitasking) {
    super.onResume(multitasking);
    if (adView != null) {
        adView.resume();
    }
}