实施Google Reward Video时遇到一个奇怪的问题。在测试模式(带有google ad-id)下,第一次完美运行,但第二次不加载视频。在生产中(带有我的ID)完全不加载视频。这是我的代码:
import android.content.DialogInterface;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import eu.healthydev.quizhero.Common.Common;
import eu.healthydev.quizhero.Model.Question;
import eu.healthydev.quizhero.Model.QuestionScore;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
public class DoneActivity extends AppCompatActivity implements RewardedVideoAdListener{
Button btnTryAgain, exit_game;
TextView txtResultScore, getTxtResultQuestion;
ProgressBar progressBar;
FirebaseDatabase database;
DatabaseReference question_score;
//VideoAd Award
private static final String AD_UNIT_ID = "ca-app-pub-5883754622051955/6179215953";
private static final String APP_ID = "ca-app-pub-5883754622051955~8837168889";
private int coinCount;
private TextView coinCountText;
private RewardedVideoAd rewardedVideoAd;
private Button showVideoButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_done);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, APP_ID);
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
rewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
database = FirebaseDatabase.getInstance();
question_score = database.getReference("Question_Score");
txtResultScore = (TextView)findViewById(R.id.txtTotalScore);
getTxtResultQuestion =(TextView)findViewById(R.id.txtTotalQuestion);
progressBar = (ProgressBar)findViewById(R.id.doneProgressBar);
// Create the "retry" button, which tries to show an interstitial between game plays.
btnTryAgain = (Button)findViewById(R.id.btnTryAgain);
exit_game = (Button) findViewById(R.id.exit_game);
btnTryAgain.setVisibility(View.INVISIBLE);
btnTryAgain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(DoneActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
});
// Create the "show" button, which shows a rewarded video if one is loaded.
showVideoButton = findViewById(R.id.show_video_button);
showVideoButton.setVisibility(View.VISIBLE);
showVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRewardedVideo();
}
});
exit_game.setVisibility(View.VISIBLE);
exit_game.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
// Display current coin count to user.
coinCountText = findViewById(R.id.coin_count_text);
coinCount = 0;
coinCountText.setText("Coins: " + coinCount);
//Get data from bundle and set view
Bundle extra = getIntent().getExtras();
if(extra != null)
{
int score = extra.getInt("SCORE");
int totalQuestion = extra.getInt("TOTAL");
int correctAnswer = extra.getInt("CORRECT");
txtResultScore.setText(String.format("SCORE : %d", score));
getTxtResultQuestion.setText(String.format("PASSED : %d /%d", correctAnswer,totalQuestion));
progressBar.setMax(totalQuestion);
progressBar.setProgress(correctAnswer);
//Upload points to Database
question_score.child(String.format("%s_%s", Common.currentUser.getUserName(),
Common.categoryID))
.setValue(new QuestionScore(String.format("%s_%s", Common.currentUser.getUserName(),
Common.categoryID),
Common.currentUser.getUserName(),
String.valueOf(score),
Common.categoryID,
Common.categoryName));
}
}
private void loadRewardedVideoAd() {
if (!rewardedVideoAd.isLoaded()) {
rewardedVideoAd.loadAd(AD_UNIT_ID, new AdRequest.Builder().build());
}
}
private void addCoins(int coins) {
coinCount += coins;
coinCountText.setText("Coins: " + coinCount);
}
private void showRewardedVideo() {
showVideoButton.setVisibility(View.INVISIBLE);
exit_game.setVisibility(View.INVISIBLE);
if (rewardedVideoAd.isLoaded()) {
rewardedVideoAd.show();
}
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
// Preload the next video ad.
loadRewardedVideoAd();
btnTryAgain.setVisibility(View.INVISIBLE);
showVideoButton.setVisibility(View.VISIBLE);
exit_game.setVisibility(View.VISIBLE);
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(this, "Ad Video Failed to Load", Toast.LENGTH_SHORT).show();
showVideoButton.setVisibility(View.VISIBLE);
exit_game.setVisibility(View.VISIBLE);
}
@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
}
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this,
String.format(" You ve got one free try!", reward.getType(),
reward.getAmount()),
Toast.LENGTH_SHORT).show();
btnTryAgain.setVisibility(View.VISIBLE);
showVideoButton.setVisibility(View.INVISIBLE);
exit_game.setVisibility(View.INVISIBLE);
addCoins(1);
Intent intent = new Intent(DoneActivity.this, HomeActivity.class);
startActivity(intent);
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finishAffinity();
System.exit(0);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
我仔细检查了我的ID,并且正确无误。也可能是我的代码,因为它完全可以正常工作。我会提供任何帮助或想法。
答案 0 :(得分:0)
发件人:https://developers.google.com/admob/android/rewarded-video
@Override
public void onRewardedVideoAdClosed() {
// Load the next rewarded video ad.
loadRewardedVideoAd();
}
第一个关闭后,我们需要重新加载。
由于相同,我的应用程序正常运行。希望有帮助。