无法在 andengine 中使用 admob 显示插页式广告

Unable to show interstitial ads with admob in andengine

我一直在尝试在我的 andengine 开发的游戏中显示插页式广告,但无法显示。不过,我已经成功地整合了横幅广告。我更喜欢使用编码进行布局。下面是我的 GameActivity.java 和 Gamescene.java 类,请帮助理解根本错误在哪里?

public class GameActivity extends BaseGameActivity
{
    private SmoothCamera camera;
    public Music music;`

    public static int flag=0;


    @Override
    public Engine onCreateEngine(EngineOptions pEngineOptions) 
    {
        return new LimitedFPSEngine(pEngineOptions, 60);
    }

    public EngineOptions onCreateEngineOptions()
    {
        camera = new SmoothCamera(0, 0, 720, 1280,5000000,5000000,1);
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(), this.camera);
        engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
        engineOptions.getRenderOptions().getConfigChooserOptions().setRequestedMultiSampling(true);
        engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
        engineOptions.getTouchOptions().setNeedsMultiTouch(true);
        return engineOptions;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {  
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
        }
        return false; 
    }

    public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException
    {
        ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
        pOnCreateResourcesCallback.onCreateResourcesFinished();


        try
        {
            music = MusicFactory.createMusicFromAsset(mEngine.getMusicManager(), this,"music/music.ogg");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    AdView adView ;@Override
    @SuppressLint("NewApi")
    protected void onSetContentView() {


        final FrameLayout frameLayout = new FrameLayout(this);
        final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT, Gravity.FILL);
        final FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER | Gravity.TOP);

        adView = new AdView(this);
        adView.setAdUnitId("admobid");
        //adView.setAdUnitId("admobid");
        adView.setAdSize(AdSize.);

        adView.setVisibility(AdView.VISIBLE);
        adView.refreshDrawableState();

        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
            adView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }


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



        final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT);
        surfaceViewLayoutParams.gravity = Gravity.CENTER;

        frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

        adView.setAdListener(new AdListener() {
            public void onAdLoaded(){

                if(flag==0)
                {flag=1;
                frameLayout.addView(adView, adViewLayoutParams);

                }
            }
        });


        this.setContentView(frameLayout, frameLayoutLayoutParams);

    }

    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
    {
        SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
    }

    public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException
    {
        mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                SceneManager.getInstance().createMenuScene();

            }
        }));
        pOnPopulateSceneCallback.onPopulateSceneFinished();
    }
    @Override
    protected void onPause() {
        SceneManager.getInstance().gameScene.onBackKeyPressed();
        super.onPause();
    }
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        System.exit(0); 
    }
}

我尝试放置插页式广告的游戏结束方法

private void gameover()
{this.gameovercheck = 1;
this.setIgnoreUpdate(true);
Sprite gameover = new Sprite(camera.getCenterX(), camera.getCenterY(), resourcesManager.gameover, vbom);
this.attachChild(gameover);
rocket.setCurrentTileIndex(2);


      activity.runOnUiThread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub

            InterstitialAd interstitial = new InterstitialAd(activity);
            interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

            AdRequest adRequest = new AdRequest.Builder().build();


            interstitial.loadAd(adRequest);

                interstitial.show();



        }

    });
soundOnBtn1 = new Sprite(camera.getCenterX(),camera.getCenterY()-200, resourcesManager.retry, vbom)
{

    public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)
    {if(pSceneTouchEvent.isActionDown())

        SceneManager.getInstance().loadGameScene1(engine);
    gameovercheck=0;
    editor.commit();
    editor.commit();
    return true;
    };
};
this.attachChild(soundOnBtn1);
this.registerTouchArea(soundOnBtn1);

您必须确保插页式广告已预加载,否则不会显示。

预加载 interstitial.loadAd(adRequest); 例如就在你开始你的场景时。

然后在您的 Gameover 方法中像这样检查 isLoaded

boolean loaded = interstitialAd.isLoaded();
if (loaded) {
  interstitial.show();
} else {
//loadyourINterstitial
}