问题描述
因此,我想制作一个带有简单按钮的应用程序,以显示奖励广告。该按钮起作用并显示奖励广告,但是当我再次按下时,没有奖励广告显示。 我想在每次按钮点击时展示广告。
这是代码:
public class MainActivity extends AppCompatActivity {
private RewardedAd rewardedAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
TextView textView = (TextView) findViewById(R.id.textView);
rewardedAd = new RewardedAd(this,"ca-app-pub-3940256099942544/5224354917");
final RewardedAdLoadCallback[] adLoadCallback = {new RewardedAdLoadCallback() {
@Override
public void onRewardedAdLoaded() {
// Ad successfully loaded.
}
@Override
public void onRewardedAdFailedToLoad(LoadAdError adError) {
// Ad Failed to load.
}
}};
rewardedAd.loadAd(new AdRequest.Builder().build(),adLoadCallback[0]);
button.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rewardedAd.isLoaded()) {
Activity activityContext = MainActivity.this;
RewardedAdCallback adCallback = new RewardedAdCallback() {
@Override
public void onRewardedAdOpened() {
// Ad opened.
}
@Override
public void onRewardedAdClosed() {
}
@Override
public void onUserEarnedReward(@NonNull RewardItem reward) {
}
@Override
public void onRewardedAdFailedToShow(AdError adError) {
// Ad Failed to display.
}
};
rewardedAd.show(activityContext,adCallback);
} else {
Log.d("TAG","The rewarded ad wasn't loaded yet.");
}
}
});
}
}
我没有设置奖励,首先我希望按钮能够显示多个广告