提问者:小点点

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


在用户关闭间隙广告后,我将在我的应用程序中添加奖励广告,它将显示一个屏幕,显示之前输入的文本,并在下方有一个显示奖励广告的视频按钮(我处于测试阶段,因此我还不包括任何奖励项目,只想先显示视频广告)。

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

我的代码:

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个答案

匿名用户

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

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  {

显示ads方法,如下所示

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