Google Reward Video の実装で奇妙な問題が発生しました。テスト モード (Google 広告 ID を使用) では、初回は完全に機能しますが、2 回目はビデオを読み込めません。本番環境 (私の 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 を再確認しましたが、正しいです。おそらく私のコードも、それがしなければならないのとまったく同じように機能するからです。私は助けやアイデアを感謝します。