您在使用Admob奖励的视频时遇到了麻烦。为了展示奖励广告,这些视频似乎被黑屏奖励了。如果我们等到宣布,我们将获得奖金。 经过多次尝试后,屏幕变黑了,但是只有完成后才能关闭广告 有人可以帮我吗?
谢谢。
我的代码:
public class AdVideo extends AppCompatActivity implements RewardedVideoAdListener {
private static final String AD_UNIT_ID = "my ad unit id";
private static final String APP_ID = "my app id";
private static final long COUNTER_TIME = 15;
private TextView mCoinCountText,timer;
private CountDownTimer mCountDownTimer;
private boolean mGameOver;
private boolean mGamePaused;
private RewardedVideoAd mRewardedVideoAd;
private Button mRetryButton;
private Button mShowVideoButton;
private long mTimeRemaining;
int idClass;
AdView mAdView;
AdRequest adRequest;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.advideo);
try {
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, APP_ID);
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
mAdView = (AdView) findViewById(R.id.adWebSchool);
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
mAdView.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad( int error ) {
mAdView.setVisibility(View.GONE);
}
});
// Create the "retry" button, which tries to show an interstitial between game plays.
mRetryButton = ((Button) findViewById(R.id.retry));
mRetryButton.setVisibility(View.INVISIBLE);
mRetryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startGame();
}
});
// Create the "show" button, which shows a rewarded video if one is loaded.
mShowVideoButton = ((Button) findViewById(R.id.viewAdVideo));
mShowVideoButton.setVisibility(View.INVISIBLE);
mShowVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRewardedVideo();
}
});
timer = ((TextView) findViewById(R.id.timer));
// Display current coin count to user.
mCoinCountText = ((TextView) findViewById(R.id.con));
mCoinCountText.setText(" :" + getSharedPreferences("Coins", Context.MODE_PRIVATE).getInt("coins", 0));
idClass = getIntent().getIntExtra("idClass", 0);
startGame();
}
catch (Exception e){
Toast.makeText(this, " (11)", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onPause() {
super.onPause();
try {
pauseGame();
mRewardedVideoAd.pause(this);
}
catch (Exception e){
Toast.makeText(this, "(12)", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onResume() {
super.onResume();
try {
if (!mGameOver && mGamePaused) {
resumeGame();
}
mRewardedVideoAd.resume(this);
}
catch (Exception e){
Toast.makeText(this, " (13)", Toast.LENGTH_SHORT).show();
}
}
private void pauseGame() {
try {
mCountDownTimer.cancel();
mGamePaused = true;
}
catch (Exception e){
Toast.makeText(this, " (14)", Toast.LENGTH_SHORT).show();
}
}
private void resumeGame() {
try {
createTimer(mTimeRemaining);
mGamePaused = false;
}
catch (Exception e){
Toast.makeText(this, "(15)", Toast.LENGTH_SHORT).show();
}
}
private void loadRewardedVideoAd() {
try {
if (!mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.loadAd(AD_UNIT_ID,new AdRequest.Builder().build() );
}
}
catch (Exception e){
Toast.makeText(this, "(16)", Toast.LENGTH_SHORT).show();
}
}
private void startGame() {
try {
// Hide the retry button, load the ad, and start the timer.
mRetryButton.setVisibility(View.VISIBLE);
mShowVideoButton.setVisibility(View.INVISIBLE);
loadRewardedVideoAd();
createTimer(COUNTER_TIME);
mGamePaused = false;
mGameOver = false;
}
catch (Exception e){
Toast.makeText(this, "(17)", Toast.LENGTH_SHORT).show();
}
}
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
private void createTimer(long time) {
try {
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
mCountDownTimer = new CountDownTimer(time * 1000, 50) {
@Override
public void onTick(long millisUnitFinished) {
timer.setVisibility(View.VISIBLE);
mTimeRemaining = ((millisUnitFinished / 1000) + 1);
timer.setText( " : "+mTimeRemaining);
}
@Override
public void onFinish() {
if (mRewardedVideoAd.isLoaded()) {
mShowVideoButton.setVisibility(View.VISIBLE);
}
timer.setVisibility(View.INVISIBLE);
mRetryButton.setVisibility(View.VISIBLE);
mGameOver = true;
}
};
mCountDownTimer.start();
}
catch (Exception e){
Toast.makeText(this, " (18)", Toast.LENGTH_SHORT).show();
}
}
private void showRewardedVideo() {
try {
mShowVideoButton.setVisibility(View.INVISIBLE);
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
catch (Exception e){
Toast.makeText(this, "(19)", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "vonRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
ToastShow("onRewardedVideoAdFailedToLoad");
}
@Override
public void onRewardedVideoCompleted() {
Toast.makeText(this, "onRewardedVideoCompleted", 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 onRewarded(RewardItem reward) {
getSharedPreferences("Coins", Context.MODE_PRIVATE).edit().putInt("coins",getSharedPreferences("Coins", Context.MODE_PRIVATE).getInt("coins", 0)+ 5).apply();
mCoinCountText.setText(" : " + getSharedPreferences("Coins", Context.MODE_PRIVATE).getInt("coins", 0));
}
@Override
public void onRewardedVideoStarted() {
}
@Override
public void onBackPressed() {
try {
Intent intent;
if (idClass == 1) {
intent = new Intent(this, ViewBook.class);
startActivity(intent);
}
if (idClass == 2) {
intent = new Intent(this, ViewSolution.class);
startActivity(intent);
}
else {
super.onBackPressed();
}
}
catch (Exception e){
Toast.makeText(this, " (110)", Toast.LENGTH_SHORT).show();
}
}
private void ToastShow( String message ) {
try {
View layoutValue = LayoutInflater.from(this).inflate(R.layout.theme_toast, null);
TextView textMessage = (TextView) layoutValue.findViewById(R.id.custom_toast_message);
textMessage.setText(message);
final Toast toast = new Toast(this);
toast.setView(layoutValue);
final CountDownTimer toastCountDown;
toastCountDown = new CountDownTimer(5000, 1000) {
public void onTick( long millisUntilFinished ) {
toast.show();
}
public void onFinish() {
toast.cancel();
}
};
toast.show();
toastCountDown.start();
Button ToastCancel = (Button) layoutValue.findViewById(R.id.ToastCancel);
ToastCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View v ) {
toastCountDown.cancel();
toast.cancel();
}
});
}
catch (Exception e){
Toast.makeText(this, " (111)", Toast.LENGTH_SHORT).show();
}
}
}