facebook twitter hatena line email

「Android/広告組込/google/admob/リワード新」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
 
行1: 行1:
==リワードサンプル旧api==
+
==リワードサンプル新api==
 
公式:https://developers.google.com/admob/android/rewarded-ads?hl=ja
 
公式:https://developers.google.com/admob/android/rewarded-ads?hl=ja
  

2020年2月18日 (火) 20:42時点における最新版

リワードサンプル新api

公式:https://developers.google.com/admob/android/rewarded-ads?hl=ja

以下記述のunitidはサンプルのid

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {

    private RewardedAd rewardedAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        rewardedAd = new RewardedAd(this,
            "ca-app-pub-3940256099942544/5224354917");
        RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
            @Override
            public void onRewardedAdLoaded() {
                // Ad successfully loaded.
            }

           @Override
            public void onRewardedAdFailedToLoad(int errorCode) {
                // Ad failed to load.
            }
        };
        rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              loadRequest();
            }
        });
        
    }
    
    RewardedAdCallback mAdCallback = new RewardedAdCallback() {
        public void onRewardedAdOpened() {
            // Ad opened.
        }

        public void onRewardedAdClosed() {
            // Ad closed.
        }

        public void onUserEarnedReward(@NonNull RewardItem reward) {
            // User earned reward.
        }

        public void onRewardedAdFailedToShow(int errorCode) {
            // Ad failed to display
        }
    };
    void loadRequest() {
      if (rewardedAd.isLoaded()) {
          // 広告表示
          rewardedAd.show(getContext(), mAdCallback);
      } else {
          Log.d("TAG", "The rewarded ad wasn't loaded yet.");
      }
    }
    
}