Admob-奖励广告已加载但未显示

时间:2019-11-22 10:43:46

标签: android admob admob-rewardedvideoad

我在用户关闭插页式广告后将奖励广告添加到我的应用程序中,它将显示一个屏幕,该屏幕显示以前输入的文字,并在其下方有一个用于奖励广告的“显示视频”按钮。 (我正处于测试阶段,因此我还不包括任何奖励物品,只想先展示视频广告即可。)

当我使用Toast小部件确定广告是否已加载时,我意识到广告已加载(显示“ onRewardedAdLoaded”文本)。但是,当我单击观看视频按钮时,似乎没有调用showRewardedVideo()。有人可以帮我看看我的代码有什么问题吗?

enter image description here

我的代码:

public class MainActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";
private AdView mAdView;
private InterstitialAd mInterstitialAd;
Button mMyButton;
private RewardedAd rewardedAd;
private Button showVideoButton;
boolean isLoading;

@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) {
        }
    });
    loadRewardedAd();
    mAdView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
    mMyButton = (Button) findViewById(R.id.button);
    mMyButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.setAdListener(new AdListener() {
                    @Override
                    public void onAdClosed() {
                        AdRequest adRequest = new AdRequest.Builder().build();
                        mInterstitialAd.loadAd(adRequest);
                        sendMessage(mAdView);
                        // Create the "show" button, which shows a rewarded video if one is loaded.
                        showVideoButton = findViewById(R.id.show_video_button);
                        showVideoButton.setVisibility(View.INVISIBLE);
                        showVideoButton.setOnClickListener(
                                new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        showRewardedVideo();
                                    }
                                });
                    }
                });
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
}

private void loadRewardedAd() {
    if (rewardedAd == null || !rewardedAd.isLoaded()) {
        rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
        isLoading = true;
        rewardedAd.loadAd(
                new AdRequest.Builder().build(),
                new RewardedAdLoadCallback() {
                    @Override
                    public void onRewardedAdLoaded() {
                        // Ad successfully loaded.
                        MainActivity.this.isLoading = false;
                        Toast.makeText(MainActivity.this, "onRewardedAdLoaded", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRewardedAdFailedToLoad(int errorCode) {
                        // Ad failed to load.
                        MainActivity.this.isLoading = false;
                        Toast.makeText(MainActivity.this, "onRewardedAdFailedToLoad", Toast.LENGTH_SHORT)
                                .show();
                    }
                });
    }
}



/**
 * Called when the user taps the Send button
 */
public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

private void showRewardedVideo() {
    showVideoButton.setVisibility(View.INVISIBLE);
    if (rewardedAd.isLoaded()) {
        RewardedAdCallback adCallback =
                new RewardedAdCallback() {
                    @Override
                    public void onRewardedAdOpened() {
                        // Ad opened.
                        Toast.makeText(MainActivity.this, "onRewardedAdOpened", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRewardedAdClosed() {
                        // Ad closed.
                        Toast.makeText(MainActivity.this, "onRewardedAdClosed", Toast.LENGTH_SHORT).show();
                        // Preload the next video ad.
                        MainActivity.this.loadRewardedAd();
                    }

                    @Override
                    public void onUserEarnedReward(RewardItem rewardItem) {
                        // User earned reward.
                        Toast.makeText(MainActivity.this, "onUserEarnedReward", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onRewardedAdFailedToShow(int errorCode) {
                        // Ad failed to display
                        Toast.makeText(MainActivity.this, "onRewardedAdFailedToShow", Toast.LENGTH_SHORT)
                                .show();
                    }
                };
        rewardedAd.show(this, adCallback);
    }
}
}

谢谢!

1 个答案:

答案 0 :(得分:0)

像这样修改您的奖励广告加载方式

private void loadRewardedAd() {
if (rewardedAd == null || !rewardedAd.isLoaded()) {
    isLoading = true;
    rewardedAd.loadAd("ca-app-pub-3940256099942544/5224354917",
        new AdRequest.Builder().build());
}
}
*****Method ends here***** //implement RewardedVideoAdListners' callback methods like this
    @Override
public void onRewarded(RewardItem reward) {
    Toast.makeText(this, "onRewarded! currency: " + reward.getType() + "  amount: " +
            reward.getAmount(), Toast.LENGTH_SHORT).show();
    // Reward the user.
}

@Override
public void onRewardedVideoAdLeftApplication() {
    Toast.makeText(this, "onRewardedVideoAdLeftApplication",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
    MainActivity.this.loadRewardedAd();
    Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
    Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLoaded() {
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdOpened() {
    Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
    Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoCompleted() {
    Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
}

然后像这样实现RewardedVideoAdListener

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener  {

如下所示的展示广告方法

private void showRewardedVideo() {
showVideoButton.setVisibility(View.INVISIBLE);
if(rewardedAd.isLoaded){
rewarded.show();
    } else {
//Do something...
}
}