首页 > 解决方案 > Android Studio - 插页式广告加载问题

问题描述

所以我正在尝试将插页式广告添加到我的项目中。

这是我的代码在当前的样子JHAActivity.java

注意:在我的项目中,我使用的是我自己的来自 apps.admob.com 的广告单元 ID ca-app-pub-1111111111111111/2222222222

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class JHAActivity extends AppCompatActivity {

    private Button jhaButton;

    // private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712"; Test AD
    private static final String AD_UNIT_ID = "ca-app-pub-1111111111111111/2222222222";

    private InterstitialAd mInterstitialAd;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // R.menu.mymenu is a reference to an xml file named mymenu.xml which should be inside your res/menu directory.
        // If you don't have res/menu, just create a directory named "menu" inside res
        getMenuInflater().inflate(R.menu.mymenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // handle button activities
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.mybutton) {
            // do something here

            if (Locale.getDefault().getLanguage().equals("en")) { // If English
                new AlertDialog.Builder(this)
                        .setTitle(R.string.rules)
                        .setMessage(rulesENG)

                        // A null listener allows the button to dismiss the dialog and take no further action.
                        .setNegativeButton(android.R.string.ok, null)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            } else { // Else if not English
                new AlertDialog.Builder(this)
                        .setTitle(R.string.rules)
                        .setMessage(rules)

                        // A null listener allows the button to dismiss the dialog and take no further action.
                        .setNegativeButton(android.R.string.ok, null)
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        }
        return super.onOptionsItemSelected(item);
    }

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        // Initialize the Mobile Ads SDK.
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(AD_UNIT_ID);
        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                // Load the next interstitial.
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }

            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }

            @Override
            public void onAdFailedToLoad(LoadAdError adError) {
                // Code to be executed when an ad request fails.
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when the ad is displayed.
            }

            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }
        });

        jhaButton = (Button) findViewById(R.id.button);
        jhaButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openJHA();
            }
        });
    }

    private void showInterstitial() {
        // Show the ad if it's ready. Otherwise toast and restart the game.
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            // Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }

    int counter = 0;

    private void openJHA() {

        counter++;

        if (counter == 5) {
            showInterstitial(); // Display AdMob AD
            counter = 0;
        }
    }
}

所以基本上当ca-app-pub-3940256099942544/1033173712用作广告单元ID时,每次按下jhaButton5次它都会显示测试广告,但是当使用我自己的广告单元ID时,广告根本不显示。在运行日志中它只是说:D/TAG: The interstitial wasn't loaded yet.。我在这里做错了什么?

标签: javaandroidadmob

解决方案


 @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdFailedToLoad(LoadAdError adError) {
            // Code to be executed when an ad request fails.
        }

mInterstitialAd.loadAd(xxx)尝试加载一次,但通常会onAdFailedToLoad(LoadAdError adError)返回,如果您没有mInterstitialAd.loadAd再次尝试尝试,它将停止并且永远不会跳转到onAdLoaded(). 因此,您mInterstitialAd.isLoaded()将几乎返回错误。

每次要使用mInterstitialAd.showAd(),请先if (!mInterstitialAd.isLoaded()){mInterstitialAd.showAd() }检查

最大的问题是最近的 Admob 使用了 SDK v19.7.0 。InterestitialAd相关的代码需要再次修改……而且我不知道为什么回调失去了关于onAdClicked.


推荐阅读