开发者社区> 问答> 正文

如何加载RewardedAd?

你好!

我想使用RewardedAd(admob)在我的应用中获得奖励。我在youtube(https://www.youtube.com/watch?v=CmrTF0hLsIk)上观看了官方的admob视频。这是我的代码:


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.");
    }
}

但是我的广告没有加载。当我尝试showAd()时,logcat会显示以下消息:

01-17 19:27:00.144 27830-27830/com.example.quiz E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.

01-17 19:27:08.968 27830-27830/com.example.quiz I/RewardActivity: Ad not loaded.

01-17 19:27:02.256 27830-27830/com.example.quiz I/Ads: This request is sent from a test device.

我正在使用Google的测试ID。我的代码有什么问题?为什么我的广告没有加载?我能做什么?

谢谢!

展开
收起
Puppet 2020-01-18 10:37:38 1259 0
1 条回答
写回答
取消 提交回答
  • 我遵循了同一教程,并检查了您的代码,这似乎是正确的,以防万一您想再次检查!在您按下载入按钮后,请尝试按几次显示按钮,因为有时广告会花费一些时间来载入。并且不要忘记从activity_main.xml设置按钮onClick函数。

    这是MainActivity:

    
      public class MainActivity extends AppCompatActivity {
    
        private RewardedAd rewardedAd;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(InitializationStatus initializationStatus) {
    
                }
            });
        }
    
        public void showAd(View view) {
    
            if(this.rewardedAd.isLoaded()){
    
                RewardedAdCallback callback = new RewardedAdCallback() {
                    @Override
                    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                       Log.i("MainActivity","Got Reward");
                    }
    
                    @Override
                    public void onRewardedAdOpened() {
                        super.onRewardedAdOpened();
                            Log.i("MainActivity","Opened");
                    }
    
                    @Override
                    public void onRewardedAdClosed() {
                        super.onRewardedAdClosed();
                        Log.i("MainActivity","Closed");
                    }
    
                    @Override
                    public void onRewardedAdFailedToShow(int i) {
                        super.onRewardedAdFailedToShow(i);
                        Log.i("MainActivity","Failed to show");
                    }
                };
                this.rewardedAd.show(this,callback);
    
            }
            else{
                Log.i("MainActivity","Not Loaded");
            }
        }
    
        public void loadAd(View view) {
    
            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("MainActivity","Failed");
                }
    
                @Override
                public void onRewardedAdLoaded() {
                    super.onRewardedAdLoaded();
                    Log.i("MainActivity","Loaded");
                }
            };
    
            this.rewardedAd.loadAd(new AdRequest.Builder().build(),callback);
        }
    }
    

    这是Android清单:

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <meta-data
                android:name="com.google.android.gms.ads.APPLICATION_ID"
                android:value="ca-app-pub-3940256099942544~3347511713"/>
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    这是问题:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.2"
        defaultConfig {
            applicationId "jalaleddine.abdelbasset.admobtest"
            minSdkVersion 19
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        implementation 'com.google.android.gms:play-services-ads:18.2.0'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    }
    
    2020-01-18 10:38:13
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载