Admob + Andengine仅在第二次请求时显示广告

时间:2014-02-19 18:40:25

标签: android admob andengine

问题是:广告没有在第一次请求时显示,但是当它发出第二个请求时,它显示正确。在发出第二个请求之前大约2秒钟,第一个请求的广告就会显示出来。有任何想法吗? 谢谢,

编辑:我将重力更改为TOP,现在它第一次显示,但是只显示广告的上半部分,奇怪,有什么想法?

@Override
protected void onSetContentView() {

    if(adView != null){
        return;
    }

    final FrameLayout frameLayout = new FrameLayout(this);
    final FrameLayout.LayoutParams frameLayoutLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                                         FrameLayout.LayoutParams.MATCH_PARENT);

    final FrameLayout.LayoutParams adViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                                         FrameLayout.LayoutParams.WRAP_CONTENT,
                                         Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);

    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
    adView.setAdListener(new ToastAdListener(this));
    adView.loadAd(new AdRequest.Builder().build());

    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine,this);

    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
            new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
    frameLayout.addView(adView, adViewLayoutParams);

    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

@Override
protected void onPause() {
    if(adView!=null){
        adView.pause();
    }
    super.onPause();
}

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

@Override
protected void onDestroy() {
    if(adView!=null){
        adView.destroy();
    }
    super.onDestroy();
}

3 个答案:

答案 0 :(得分:2)

通过添加该行解决了我的问题 adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);

答案 1 :(得分:1)

我的方式如下:

  1. 在更改可见性的方法中,我首先检查广告是否已加载。如果不是,我不会改变可见性
  2. AdListener设置AdView。在onReceivedAd()中,我检查条件是否隐藏它 - 如果它应该隐藏,我隐藏。
  3. 这样做很好。

答案 2 :(得分:0)

我遇到了同样的问题,我这样解决了:

广告类:

public class Advertisement {

    private AdView adView;
    private final Handler adsHandler = new Handler();

    public Advertisement(final Activity activity) {
    adView = (AdView)activity.findViewById(R.id.adView);        
    }

    //show the ads.
    private void showAds () {
        adView.loadAd(new AdRequest.Builder().build());
        adView.setVisibility(android.view.View.VISIBLE);
        adView.setEnabled(true);
    }

    //hide ads.
    private void unshowAds () {
        adView.loadAd(new AdRequest.Builder().build());
        adView.setVisibility(android.view.View.INVISIBLE);
        adView.setEnabled(false);
    }

    final Runnable unshowAdsRunnable = new Runnable() {
        public void run() {
            unshowAds();
        }
    };

    final Runnable showAdsRunnable = new Runnable() {
        public void run() {
            showAds();
        }
    };

    public void showAdvertisement() {
        adsHandler.post(showAdsRunnable);
    }

    public void hideAdvertisement() {
        adsHandler.post(unshowAdsRunnable);
    }
}

在代码中:

...
public static Advertisement advertisement = new Advertisement(this);
...
public static void showAd() 
{
    if (advertisement != null) 
        advertisement.showAdvertisement();
}
public static void hideAd() 
{
    if (gangsterAdvertisement != null) 
        advertisement.hideAdvertisement();
}