Question Detail


Thread Reply

alex levine

- 2 months ago

Manifest.xml

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-UUUUUUUUU~YYYYYYY" />

 

build.gradle

implementation 'com.google.android.gms:play-services-ads:15.0.1'

 

VideoAdClass.java

import android.app.Activity;
import android.util.Log;
import tvs.app.neofitness.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;

public class VideoAdsClass implements RewardedVideoAdListener {
    String TAG = "VideoAdsClass";
    Activity mActivity;
    RewardedVideoAd mAd;

    public VideoAdsClass(Activity activity) {
        this.mActivity = activity;
        mAd = MobileAds.getRewardedVideoAdInstance(mActivity);
        mAd.setRewardedVideoAdListener(this);
        loadRewardedVideo();
    }

    private void loadRewardedVideo() {
        mAd.loadAd(mActivity.getString(R.string.videoads),new AdRequest.Builder().build());
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        Log.i(TAG, "Rewarded: onRewardedVideoAdLoaded");
        try {
            if (mAd.isLoaded()) {
                mAd.show();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRewardedVideoAdOpened() {
        Log.i(TAG, "Rewarded: onRewardedVideoAdOpened");
    }

    @Override
    public void onRewardedVideoStarted() {
        Log.i(TAG, "Rewarded: onRewardedVideoStarted");
    }

    @Override
    public void onRewardedVideoAdClosed() {
        Log.i(TAG, "Rewarded: onRewardedVideoAdClosed");
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {
        Log.i(TAG, "Rewarded:  onRewarded! currency: " + rewardItem.getType() + "  amount: " +
                rewardItem.getAmount());

    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
        Log.i(TAG, "Rewarded: onRewardedVideoAdLeftApplication ");
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {
        Log.i(TAG, "Rewarded: onRewardedVideoAdFailedToLoad: " + i);

    }

    @Override
    public void onRewardedVideoCompleted() {

    }
}