libGDX中的奖励视频广告示例

时间:2017-06-06 19:06:26

标签: android libgdx admob rewardedvideoad

我一直在寻找一个例子,但没有找到它。

我发现一个没有libgdx,有些为什么它不起作用,但app也没有崩溃。

public class AndroidLauncher extends AndroidApplication implements AdHandler, RewardedVideoAdListener{

    ...

    RewardedVideoAd rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    rewardedVideoAd.setRewardedVideoAdListener(this);

    rewardedSetup();

    private void rewardedSetup(){
        if (!rewardedVideoAd.isLoaded()){
            rewardedVideoAd.loadAd("ca-app-pub-683510441556959xxxxxxxxx", new AdRequest.Builder().build());
        }

        @Override
        public void onRewardedVideoAdLoaded() {
            if(rewardedVideoAd.isLoaded()){
                rewardedVideoAd.show();
            }
        }

        @Override
        public void onRewarded(RewardItem rewardItem) {
            rewarded(true);
        }
    }
}

它只是没有显示,也许它发生在任何人身上? (我的真正的代码充满了处理程序和东西,因此我没有把它全部,我猜其他代码对这个问题有0效果)

1 个答案:

答案 0 :(得分:2)

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);
}

设置完成后,需要两个方法即AdHandler的实现,这两个方法将从core模块调用。

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.
}

致电hasVideoReward(),告诉您是否显示奖励面板,showVideoAd()方法显示奖励视频。

希望您在AndroidManifest.xml文件中有这些条目

<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" />