用户关闭插页式广告后,我将奖励广告添加到我的应用程序中,它将显示一个屏幕,显示之前输入的文本,并在其下方有一个显示视频按钮用于奖励广告。(我正处于测试阶段,因此我还没有包含任何奖励项目,只想先展示视频广告)。
当我使用 Toast 小部件来识别广告是否已加载时,我意识到广告已加载(显示“onRewardedAdLoaded”文本)。但是,当我单击观看视频按钮时,似乎未调用 showRewardedVideo()。有人可以帮我看看我的代码有什么问题吗?
我的代码:
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);
}
}
}
谢谢!