「Android/広告組込/google/admob/リワード旧」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→リワードサンプル古いapi) |
|||
(同じ利用者による、間の3版が非表示) | |||
行1: | 行1: | ||
==リワードサンプル旧api== | ==リワードサンプル旧api== | ||
公式:https://developers.google.com/admob/android/rewarded-video?hl=ja | 公式:https://developers.google.com/admob/android/rewarded-video?hl=ja | ||
+ | |||
+ | 以下記述のunitidはサンプルのid | ||
+ | |||
<pre> | <pre> | ||
import com.google.android.gms.ads.AdRequest; | import com.google.android.gms.ads.AdRequest; | ||
行6: | 行9: | ||
import com.google.android.gms.ads.reward.RewardedVideoAd; | import com.google.android.gms.ads.reward.RewardedVideoAd; | ||
− | public class MainActivity extends AppCompatActivity | + | public class MainActivity extends AppCompatActivity { |
private RewardedVideoAd mRewardedVideoAd; | private RewardedVideoAd mRewardedVideoAd; | ||
@Override | @Override | ||
行14: | 行17: | ||
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); | MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); | ||
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); | mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); | ||
− | mRewardedVideoAd.setRewardedVideoAdListener( | + | mRewardedVideoAd.setRewardedVideoAdListener(mListener); |
loadRewardedVideoAd(); | loadRewardedVideoAd(); | ||
} | } | ||
行38: | 行41: | ||
super.onDestroy(); | super.onDestroy(); | ||
} | } | ||
− | + | // イベント | |
− | + | private RewardedVideoAdListener mListener = new RewardedVideoAdListener() { | |
+ | @Override | ||
+ | public void onRewarded(RewardItem reward) { | ||
+ | Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " + | ||
+ | reward.getAmount(), Toast.LENGTH_SHORT).show(); | ||
+ | // Reward the user. | ||
+ | } | ||
− | + | @Override | |
− | + | public void onRewardedVideoAdLeftApplication() { | |
− | @Override | + | Toast.makeText(this, "onRewardedVideoAdLeftApplication", |
− | public void | + | Toast.LENGTH_SHORT).show(); |
− | + | } | |
− | + | ||
− | + | ||
− | } | + | |
− | @Override | + | @Override |
− | public void | + | public void onRewardedVideoAdClosed() { |
− | + | Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); | |
− | + | } | |
− | } | + | |
− | @Override | + | @Override |
− | public void | + | public void onRewardedVideoAdFailedToLoad(int errorCode) { |
− | + | Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show(); | |
− | } | + | } |
− | @Override | + | @Override |
− | public void | + | public void onRewardedVideoAdLoaded() { |
− | + | Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show(); | |
− | } | + | } |
− | @Override | + | @Override |
− | public void | + | public void onRewardedVideoAdOpened() { |
− | + | Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show(); | |
− | } | + | } |
− | @Override | + | @Override |
− | public void | + | public void onRewardedVideoStarted() { |
− | + | Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); | |
− | } | + | } |
− | @Override | + | @Override |
− | public void | + | public void onRewardedVideoCompleted() { |
− | + | Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); | |
− | } | + | } |
− | + | } | |
− | + | ||
− | + | ||
− | + | ||
} | } | ||
</pre> | </pre> |
2020年1月24日 (金) 15:15時点における最新版
リワードサンプル旧api
公式:https://developers.google.com/admob/android/rewarded-video?hl=ja
以下記述のunitidはサンプルのid
import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.reward.RewardedVideoAd; public class MainActivity extends AppCompatActivity { private RewardedVideoAd mRewardedVideoAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this); mRewardedVideoAd.setRewardedVideoAdListener(mListener); loadRewardedVideoAd(); } private void loadRewardedVideoAd() { mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build()); } @Override public void onResume() { mRewardedVideoAd.resume(this); super.onResume(); } @Override public void onPause() { mRewardedVideoAd.pause(this); super.onPause(); } @Override public void onDestroy() { mRewardedVideoAd.destroy(this); super.onDestroy(); } // イベント private RewardedVideoAdListener mListener = new RewardedVideoAdListener() { @Override public void onRewarded(RewardItem reward) { Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " + reward.getAmount(), Toast.LENGTH_SHORT).show(); // Reward the user. } @Override public void onRewardedVideoAdLeftApplication() { Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdClosed() { Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoAdFailedToLoad(int errorCode) { Toast.makeText(this, "onRewardedVideoAdFailedToLoad", 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 onRewardedVideoStarted() { Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show(); } @Override public void onRewardedVideoCompleted() { Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show(); } } }
広告表示
if (mRewardedVideoAd.isLoaded()) { mRewardedVideoAd.show(); }