计数器在admob interstitial上游戏

时间:2015-03-14 13:03:17

标签: timer admob counter interstitial

请我有一个游戏代码,其中集成了admob插页式广告。但每次玩家失败都会显示出来。我希望它每3到4次播放一次。

这是MainActitivity代码:

/** 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();
    }

1 个答案:

答案 0 :(得分: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" />