rewarded video ad example in libGDX

你说的曾经没有我的故事 提交于 2019-12-03 21:56:00
private static final String APP_ID="ca-app-pub-XXXXXX~XXXXX";
private static final String AD_UNIT_ID="ca-app-pub-XXXXX/XXXXXXX";
private RewardedVideoAd mAd;
private boolean isRewardLoaded;    

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    MobileAds.initialize(this, APP_ID);

    mAd = MobileAds.getRewardedVideoAdInstance(this);
    mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            isRewardLoaded =true;
        }

        @Override
        public void onRewardedVideoAdOpened() {

        }

        @Override
        public void onRewardedVideoStarted() {

        }

        @Override
        public void onRewardedVideoAdClosed() {
            loadRewardedVideoAd();
        }

        @Override
        public void onRewarded(RewardItem rewardItem) {

            // call rewards method from here.
            loadRewardedVideoAd();  // Load for next Reward Point

        }

        @Override
        public void onRewardedVideoAdLeftApplication() {

        }

        @Override
        public void onRewardedVideoAdFailedToLoad(int i) {

        } 

        @Override
        public void onRewardedVideoCompleted() {

        }
    });
    loadRewardedVideoAd();
}

private void loadRewardedVideoAd() {
    isRewardLoaded=false;
    mAd.loadAd(AD_UNIT_ID, new AdRequest.Builder().build());
}

@Override
protected void onResume() {
    super.onResume();
    mAd.resume(this);
}

@Override
protected void onPause() {
    super.onPause();
    mAd.pause(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mAd.destroy(this);
}

set-up done, need two methods that is implementation of AdHandler, these two methods will call from core module.

public void showVideoAd(){
        runOnUiThread(new Runnable() {
            public void run() {

                if (mAd.isLoaded()) {
                   mAd.show();
                } else {
                    loadRewardedVideoAd();
                }
            }
        });
}

public boolean hasVideoReward(){

    return isRewardLoaded; 
    //return mAd.isLoaded();    // -> must be called on the main UI thread.
}

Call hasVideoReward(), that tell you to show reward panel or not and showVideoAd() method show Reward Video.

Hopefully you've these entries in your AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<meta-data android:name="com.google.android.gms.version"
           android:value="@integer/google_play_services_version" />

<activity android:name="com.google.android.gms.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          android:theme="@android:style/Theme.Translucent" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!