简体   繁体   English

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

[英]android - interstitial ads only pop up on emulator

In my app I want to show interstitial ads. 我想在我的应用中展示插页式广告。 After 10 games being played it should be shown, so I set aa counter to see how many games have been played. 玩了10场游戏后,应该显示它,所以我设置了一个计数器来查看玩了多少场游戏。 On my emulator the int playcount; 在我的模拟器上, int playcount; resets and AdMobs test ad pops up. 重置并弹出AdMobs测试广告。 On a real device the int playcount keeps counting, and no ad is shown. 在真实设备上, int playcount计数不断计数,并且没有广告展示。 What might be wrong with it? 它可能有什么问题?

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

Use this function to show ad instead of: 使用此功能来展示广告,而不是:

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

Problem is you are using the device id to in request ad and the device id is of emulator that's why the ad is only visible in the emulator. 问题是您使用设备ID来请求广告,而设备ID是模拟器的,这就是为什么广告仅在模拟器中可见的原因。

Use this code. 使用此代码。

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

This part of your code seems a bit odd. 您的代码的这一部分似乎有些奇怪。

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 is being added a listener when playcount reaches a value of 10, everytime. minterstittialAd playcount minterstittialAd达到10时, minterstittialAd都会被添加为一个侦听器。 Also, the value of playcount after reset isn't being saved into sharedpreference. 另外,重置后的playcount计数值不会保存到sharedpreference中。

Remove the mInterstitialAd.setAdListener() , save the reset value of playcount into sharedpreferences and keep the remaining code as it is. 删除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();
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM