libGDX 中的奖励视频广告示例

rewarded video ad example in libGDX(libGDX 中的奖励视频广告示例)

本文介绍了libGDX 中的奖励视频广告示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been looking for an example, but didn't found it.

我找到了一个没有 libgdx 的软件,以及一些为什么它不起作用,但应用程序也没有崩溃.

I found one without libgdx and some why it didn't work, but app also didn't crash.

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)

And it simply don't shows, maybe it happened to anyone ? (My true code is full of handlers and stuff, thus I didn't put it all, I guess other code has 0 effect on this problem)

推荐答案

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模块调用.

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

调用hasVideoReward(),告诉你是否显示奖励面板和showVideoAd()方法显示奖励视频.

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

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

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

这篇关于libGDX 中的奖励视频广告示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:libGDX 中的奖励视频广告示例

基础教程推荐