【问题标题】:counter in admob interstitial on gameover游戏结束时在 admob 插页式广告中反击
【发布时间】:2015-03-14 13:03:16
【问题描述】:

请给我一个游戏代码,集成了 admob 插页式广告。但它显示每次玩家输掉。我希望它显示每 3 或 4 次玩家失败。

这是 MainActivity 代码:

/** The Admob ad. */
    private InterstitialAd interstitialAd = null;
    public AdView adView = null;

    public static MainActivity app;

    public void onCreate(Bundle savedInstanceState)
    {
        app = this;

        super.onCreate(savedInstanceState);

        // set view
        mGLSurfaceView = new CCGLSurfaceView(this);


        //Ads ----------------
        // Create the adView
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        //<!-- Ads Using Google Play Services SDK -->
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        // Add the adView to it
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

        adView.setLayoutParams(params);

        layout.addView(mGLSurfaceView);
        layout.addView(adView);

        setContentView(layout);
        //New AdRequest 
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        //-----------------------------------------------------Interstitial Add
        // Create an Interstitial ad.
        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(AD_INTERSTITIAL_UNIT_ID);
        interstitialAd.setAdListener(new AdListener() {
              @Override
              public void onAdLoaded() {
                interstitialAd.show();
              }

              @Override
              public void onAdFailedToLoad(int errorCode) {
                  Toast.makeText(getApplicationContext(), "Interstitial Ads loading failed", Toast.LENGTH_SHORT).show();
              }
        });
         // Load the interstitial ad.
        //showInterstitialAds();

        //----------------------
        // set director
        CCDirector director = CCDirector.sharedDirector();
        director.attachInView(mGLSurfaceView);
        director.setAnimationInterval(1/60);

        // get display info
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        G.display_w = displayMetrics.widthPixels;
        G.display_h = displayMetrics.heightPixels;
        G.scale = Math.max(G.display_w/1280.0f, G.display_h/800.0f);
        G.width = G.display_w / G.scale;
        G.height = G.display_h / G.scale;

        // get data
        SharedPreferences sp = CCDirector.sharedDirector().getActivity().getSharedPreferences("GameInfo", 0);
        G.music = sp.getBoolean("music", true);
        G.sound = sp.getBoolean("sound", true);

        // create sound
        G.soundMenu = MediaPlayer.create(this, R.raw.menu);
        G.soundMenu.setLooping(true);
        G.soundGame = MediaPlayer.create(this, R.raw.game);
        G.soundGame.setLooping(true);
        G.soundCollide = MediaPlayer.create(this, R.raw.collide);
        G.soundJump = MediaPlayer.create(this, R.raw.jump);
        G.soundLongJump = MediaPlayer.create(this, R.raw.long_jump);
        G.soundSpeedDown = MediaPlayer.create(this, R.raw.speed_down);
        G.soundSpeedUp = MediaPlayer.create(this, R.raw.speed_up);
        G.soundDirection = MediaPlayer.create(this, R.raw.direction_sign);
        G.soundClick = MediaPlayer.create(this, R.raw.menu_click);
        G.soundCollect = MediaPlayer.create(this, R.raw.collect);
        G.bgSound = G.soundMenu;

        // show menu
        CCScene scene = CCScene.node();
        scene.addChild(new MenuLayer(true));
        director.runWithScene(scene);
    }  

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

        super.onPause();
        G.bgSound.pause();
        CCDirector.sharedDirector().onPause();
    }

    @Override
    public void onResume()
    {
        super.onResume();

        if (adView != null) {
            adView.resume();
          }

        if( G.music ) G.bgSound.start();

        CCDirector.sharedDirector().onResume();
    }

    @Override
    public void onDestroy()
    {
        // Destroy the AdView.
        if (adView != null) {
          adView.destroy();
        }

        super.onDestroy();
        G.bgSound.pause();
        CCDirector.sharedDirector().end();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
            CCDirector.sharedDirector().onKeyDown(event);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void showInterstitialAds()

    {
        runOnUiThread(new Runnable() {
            public void run() {
                 AdRequest interstitialAdRequest = new AdRequest.Builder().build();
                 interstitialAd.loadAd(interstitialAdRequest);
            }
        });
    }
}

这是我的 GameOver 代码:

public void gameOver()
    {
        _mask.runAction(CCFadeIn.action(0.6f));
        _msg.setTexture(CCTextureCache.sharedTextureCache().addImage("game/failed_logo.png"));
        _msg.runAction(CCSequence.actions(
            CCEaseElasticOut.action(CCMoveTo.action(0.6f, G.displayCenter()), 0.5f), 
            CCDelayTime.action(0.5f),
            CCEaseElasticIn.action(CCMoveTo.action(0.6f, CGPoint.ccp(-G.width*0.5f, G.height*0.5f)), 0.5f), 
            CCCallFunc.action(this, "restart")));

        MainActivity.app.showInterstitialAds();
    }

【问题讨论】:

  • 只需创建一个 int,例如 adCounter,并在 gameOver 中每次递增。此外,每次在 gameOver 中检查 adCounter == 4 或您喜欢的任何数字。如果是,则显示一个广告并将 adCounter 设置回 0。
  • 你能告诉我我必须把代码放在哪里吗??
  • @samfun,看我的回答
  • @SamuelGhartey 我不知道你给我的代码是什么意思,连接到互联网时是什么意思?他必须断开连接并连接到互联网才能显示插页式广告?
  • 它只在展示广告和递增之前检查互联网连接是否可用。没有它,即使用户没有连接到互联网,计数器也会增加

标签: timer admob counter interstitial


【解决方案1】:

您可以使用计数器在特定时间展示广告。例如,在连接到互联网时仅在第 3、6、9、12 ...次展示广告:

int adCounter = 1;

   public void onCreate(Bundle savedInstanceState)

{
    app = this;

    super.onCreate(savedInstanceState);
    loadTimeAd();
    -----------
    -----------
}
private void loadTimedAd() {
        SharedPreferences sharedPrefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        adCounter = sharedPrefs.getInt("prefAdCounter", 0);


    }

 public void showInterstitialAds()

    {
if (adCounter % 3 == 0 && connectedToNet()){
        runOnUiThread(new Runnable() {
            public void run() {
                 AdRequest interstitialAdRequest = new AdRequest.Builder().build();
                 interstitialAd.loadAd(interstitialAdRequest);
            }
        });
}else PreferenceManager.getDefaultSharedPreferences(this).edit()
                        .putInt("prefAdCounter", ++adCounter).commit();
    }

//method to check if internet is connected

private boolean connectedToNet() {
        final ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();

        if (activeNetwork != null && activeNetwork.isConnected())
            return true;
        else
            return false;
    }

这需要权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    相关资源
    最近更新 更多