首页 > 解决方案 > 奖励广告未加载

问题描述

你好,

我正在尝试展示奖励广告,但无法加载。我遵循了 AdMob 的官方教程(https://www.youtube.com/watch?v=CmrTF0hLsIk&t)。这是错误:

E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.

I/Ads: This request is sent from a test device.

这是我的代码:

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

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });

    loadAd();
}

public void loadAd(){

    this.rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
    RewardedAdLoadCallback callback = new RewardedAdLoadCallback(){

        @Override
        public void onRewardedAdFailedToLoad(int i) {
            super.onRewardedAdFailedToLoad(i);

            Log.i(TAG, "OnRewardedAdFailedToLoad");
        }

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

            Log.i(TAG, "OnRewardedTagLoaded");
        }
    };
    this.rewardedAd.loadAd(new AdRequest.Builder().build(), callback);
}



public void showAd(){

    if (this.rewardedAd.isLoaded()){

        RewardedAdCallback callback = new RewardedAdCallback() {
            @Override
            public void onUserEarnedReward(@NonNull RewardItem rewardItem) {

                Log.i(TAG, "OnUserEarnedReward");
            }

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

                Log.i(TAG, "OnRewardAdOpened");
            }

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

                Log.i(TAG, "OnRewardAdClosed");
            }

            @Override
            public void onRewardedAdFailedToShow(int i) {
                super.onRewardedAdFailedToShow(i);

                Log.i(TAG, "OnRewardedAdFailedToShow");
            }
        };
        this.rewardedAd.show(this, callback);

    } else {

        Log.i(TAG, "Ad not loaded.");
    }
}

使用的所有 AdMob ID 仅用于测试,我在 AdMob 文档中获得了它们

在清单中,我添加了:

<meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713" />

构建.gradle:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'

implementation 'com.google.android.gms:play-services-ads:18.2.0'
implementation 'com.google.firebase:firebase-database:17.0.0'

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'

请帮助我,我只需要解决这个问题即可完成我的应用程序。我已经陷入这个错误两周了,我很累,我正在考虑放弃。

标签: androidadmob

解决方案


解决方案:

错误在模拟器中!要加载广告,我们需要 wi-fi 连接。API =< 25 的 AVD 无法连接到 wi-fi。所以我刚刚安装了 Android Oreo 并且它工作正常。


推荐阅读