AndroidStudioを使ってAndroidアプリの作成をしています。
リワードが始まりません。
AndroidManifestとbuild.gradleには、テストコード挿入済みです。
エラーなしです。
ソースコード
Java
1package com.example.douga; 2 3import androidx.appcompat.app.AppCompatActivity; 4 5import android.os.Bundle; 6import android.os.CountDownTimer; 7import android.util.Log; 8import android.widget.TextView; 9import android.widget.Toast; 10 11import com.google.android.gms.ads.AdRequest; 12import com.google.android.gms.ads.MobileAds; 13import com.google.android.gms.ads.reward.RewardItem; 14import com.google.android.gms.ads.reward.RewardedVideoAd; 15import com.google.android.gms.ads.reward.RewardedVideoAdListener; 16import com.google.android.gms.ads.rewarded.RewardedAd; 17 18public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener { 19 20 private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917"; 21 private static final String APP_ID = "ca-app-pub-3940256099942544~3347511713"; 22 23 private static final String TAG = "MainActivity"; 24 TextView textView; 25 private CountDownTimer mCountDownTimer; 26 private static long SET_TIME = 10; 27 private long timeRemaining; 28 private RewardedVideoAd rewardedVideoAd; 29 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 36 37 38 textView = (TextView) findViewById(R.id.textView); 39 40 MobileAds.initialize(this, APP_ID); 41 rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);/// 42 rewardedVideoAd.setRewardedVideoAdListener(this);/// 43 loadRewardedVideoAd(); 44 45 timer(SET_TIME); 46 } 47 private void timer(long time) { 48 mCountDownTimer = new CountDownTimer(time * 1000, 1000) { 49 50 public void onTick(long millisUntilFinished) { 51 timeRemaining = millisUntilFinished/1000; 52 textView.setText("seconds remaining: " + timeRemaining); 53 } 54 55 public void onFinish() { 56 textView.setText("done!"); 57 //動画広告 58 showRewardedVideo(); 59 60 } 61 62 }.start(); 63 } 64 65 private void loadRewardedVideoAd() { 66 if (!rewardedVideoAd.isLoaded()) { 67 rewardedVideoAd.loadAd(AD_UNIT_ID, new AdRequest.Builder().build()); 68 } 69 } 70 private void showRewardedVideo() { 71 if (rewardedVideoAd.isLoaded()) { 72 rewardedVideoAd.show(); 73 } 74 } 75 76 @Override 77 protected void onStart() { 78 super.onStart(); 79 Log.i(TAG, "★onStart"); 80 81 } 82 83 @Override 84 protected void onRestart(){ 85 super.onRestart(); 86 Log.i(TAG, "★onRestart"); 87 timer(timeRemaining); 88 } 89 90 @Override 91 protected void onResume() { 92 super.onResume(); 93 Toast.makeText(this, "onResume", Toast.LENGTH_SHORT).show(); 94 Log.i(TAG, "★onResume"); 95 96 } 97 98 @Override 99 protected void onPause() { 100 super.onPause(); 101 Toast.makeText(this, "onPause", Toast.LENGTH_SHORT).show(); 102 Log.i(TAG, "★onPause"); 103 104 } 105 106 @Override 107 protected void onStop() { 108 super.onStop(); 109 Toast.makeText(this, "onStop", Toast.LENGTH_SHORT).show(); 110 Log.i(TAG, "★onStop"); 111 112 mCountDownTimer.cancel(); 113 } 114 115 //広告の準備完了 116 @Override 117 public void onRewardedVideoAdLoaded() { 118 Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); 119 Log.i(TAG,"onRewardedVideoAdLoaded : "); 120 } 121 122 //広告へ移動するとき 123 @Override 124 public void onRewardedVideoAdOpened() { 125 Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); 126 Log.i(TAG,"onRewardedVideoAdOpened : "); 127 } 128 129 //広告再生 130 @Override 131 public void onRewardedVideoStarted() { 132 Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); 133 Log.i(TAG,"onRewardedVideoStarted : "); 134 } 135 136 @Override 137 public void onRewardedVideoAdClosed() { 138 Toast.makeText(this, "広告視聴ありがとうございました", Toast.LENGTH_SHORT).show(); 139 Log.i(TAG,"onRewardedVideoAdClosed : "); 140 141 } 142 143 @Override 144 public void onRewarded(RewardItem rewardItem) { 145 Toast.makeText(this, "Yes! I GOT", Toast.LENGTH_SHORT).show(); 146 Log.i(TAG,"onRewarded : "); 147 } 148 149 //広告から離れるとき 150 @Override 151 public void onRewardedVideoAdLeftApplication() { 152 Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); 153 Log.i(TAG,"onRewardedVideoAdLeftApplication : "); 154 } 155 156 @Override 157 public void onRewardedVideoAdFailedToLoad(int i) { 158 Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show(); 159 Log.i(TAG,"onRewardedVideoAdFailedToLoad : "); 160 } 161 162 @Override 163 public void onRewardedVideoCompleted() { 164 Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); 165 Log.i(TAG,"onRewardedVideoCompleted : "); 166 } 167}
XML
1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/textView" 11 android:textSize="25dp" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="Hello World!" 15 app:layout_constraintBottom_toBottomOf="parent" 16 app:layout_constraintLeft_toLeftOf="parent" 17 app:layout_constraintRight_toRightOf="parent" 18 app:layout_constraintTop_toTopOf="parent" /> 19 20 <Button 21 android:id="@+id/myButton" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="Button" 25 app:layout_constraintBottom_toBottomOf="parent" 26 app:layout_constraintEnd_toEndOf="parent" 27 app:layout_constraintStart_toStartOf="parent" 28 app:layout_constraintTop_toBottomOf="@+id/textView" /> 29 30</androidx.constraintlayout.widget.ConstraintLayout>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。