facebook twitter hatena line email

Android/広告組込/google/admob/リワード新

提供: 初心者エンジニアの簡易メモ
2020年1月24日 (金) 15:26時点におけるAdmin (トーク | 投稿記録)による版

移動: 案内検索

リワードサンプル旧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) {
            
            }
        });
        
    }
    
    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 load() {
      if (rewardedAd.isLoaded()) {
          // 広告表示
          rewardedAd.show(getContext(), mAdCallback);
      } else {
          Log.d("TAG", "The rewarded ad wasn't loaded yet.");
      }
    }
    
}