android - 插页式广告只会在模拟器上弹出

时间:2017-01-09 10:21:00

标签: java android admob

在我的应用中,我想展示插页式广告。在玩了10场比赛后,它应该会显示出来,所以我设置了一个计数器来查看已经玩了多少场比赛。在我的模拟器上,int playcount;重置并弹出AdMobs测试广告。在真实设备上,int playcount会继续计数,并且不会显示任何广告。可能有什么问题?

public class GameScreen extends Activity {
public int playcount;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);    

    TextView playctv = (TextView) findViewById(R.id.textView);
    mInterstitialAd = new InterstitialAd(this);

    // set the ad unit ID
    mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));

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

    // Load ads into Interstitial Ads
    mInterstitialAd.loadAd(adRequest);


    SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey",
            Context.MODE_PRIVATE);
    playcount = prefsplay.getInt("play_number", 0);
    playctv.setText(String.valueOf(playcount));

    AdView mAdView = (AdView) findViewById(R.id.adView);
    MobileAds.initialize(getApplicationContext(), "ca-app-pub-xxxxx");
    AdRequest adRequestd = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();



}

public void restart(View view) {
    Intent intent = new Intent(this, GameScreen.class);
    startActivity(intent);
    playcount++;

    SharedPreferences prefsplay = this
            .getSharedPreferences("myPrefsKey",
                    Context.MODE_PRIVATE);
    prefsplay.edit().putInt("play_number", playcount)
            .apply();

}


private void showInterstitial() {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }
}



public void gameover() {

    if(playcount == 10) {

    mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
                playcount = 0;
            }
        });
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                // Check the LogCat to get your test device ID
                .addTestDevice("C04B1BFFB0774708339BC273F8A43708")
                .build();
    }
}

3 个答案:

答案 0 :(得分:2)

private void adView() {
    mAdView = (AdView) findViewById(R.id.activity_ad_view);
    MobileAds.initialize(getApplicationContext(), getResources().getString(R.string.admob_banner_id));
    final AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
}

使用此功能显示广告而不是:

AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                // Check the LogCat to get your test device ID
                .addTestDevice("C04B1BFFB0774708339BC273F8A43708")
                .build();

问题是您在请求广告中使用了设备ID,而设备ID是模拟器,这就是为什么广告只能在模拟器中看到。

答案 1 :(得分:1)

使用此代码。

package com.reducephotofilesize;
import android.os.Bundle;
import android.widget.TextView;

import com.google.android.gms.ads.InterstitialAd;

public class GameScreen extends Activity {
    public int playcount;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        TextView playctv = (TextView) findViewById(R.id.textView);
        loadAd();
        SharedPreferences prefsplay = this.getSharedPreferences("myPrefsKey",
                Context.MODE_PRIVATE);
        playcount = prefsplay.getInt("play_number", 0);
        playctv.setText(String.valueOf(playcount));

        AdView mAdView = (AdView) findViewById(R.id.adView);
        MobileAds.initialize(getApplicationContext(), "ca-app-pub-xxxxx");
        AdRequest adRequestd = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();


    }

    private void loadAd() {
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));

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

        // Load ads into Interstitial Ads
        mInterstitialAd.loadAd(adRequest);
    }
}

    public void restart(View view) {
        Intent intent = new Intent(this, GameScreen.class);
        startActivity(intent);
        playcount++;

        SharedPreferences prefsplay = this
                .getSharedPreferences("myPrefsKey",
                        Context.MODE_PRIVATE);
        prefsplay.edit().putInt("play_number", playcount)
                .apply();

    }


    private void showInterstitial() {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
    }


    public void gameover() {

        if (playcount == 10) {
            showInterstitial();
            playcount = 0;
            loadAd();
        }
    }

答案 2 :(得分:0)

这部分代码似乎有些奇怪。

if(playcount == 10) {

    mInterstitialAd.setAdListener(new AdListener() {
            public void onAdLoaded() {
                showInterstitial();
                playcount = 0;
            }
        });
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                // Check the LogCat to get your test device ID
                .addTestDevice("C04B1BFFB0774708339BC273F8A43708")
                .build();
    }
每次minterstittialAd达到10时,

playcount都会添加一个监听器。此外,重置后playcount的值不会保存到共享偏好中。

删除mInterstitialAd.setAdListener(),将playcount的重置值保存到sharedpreferences中,并保留其余代码。

public void gameover() {
      if(playcount == 10) {
          showInterstitial();    
          playcount = 0;

          SharedPreferences prefsplay = getApplicationContext().getSharedPreferences("myPrefsKey",Context.MODE_PRIVATE);
          prefsplay.edit().putInt("play_number", playcount).apply();


          AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                // Check the LogCat to get your test device ID
                .addTestDevice("C04B1BFFB0774708339BC273F8A43708")
                .build();
      }
}