加载奖励视频广告时显示进度栏 [英] Show a progress Bar when Rewarded video Ads is loading

查看:79
本文介绍了加载奖励视频广告时显示进度栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用奖励视频广告(Admob),但我想在加载视频广告时显示进度栏
我已经尝试使用异步任务来做到这一点,只是看视频是否可以加载,但无法正常工作

I want to use Rewarded video ads (Admob) but I want to show a progress bar while the video ads is loading
I already try to did it with async task just to see if the video will load but it didn't work

     @SuppressLint("StaticFieldLeak")
    public class videoAd extends AsyncTask<Void, Void, Void> {

        @Override
        protected void doInBackground(Void... voids) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
                }
            });
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
            if (mRewardedVideoAd.isLoaded()){

                Toast.makeText(SetFullWallpaper.this, "Video loaded", Toast.LENGTH_SHORT).show();
                mRewardedVideoAd.show();
            }
        }
    }

现在,如果尚未加载视频,我想加载进度条
谢谢

Now I want to load a progress bar if the video is not loaded yet
Thank you

推荐答案

这是我的方法:

我有一个按钮,当单击该按钮时会显示广告,因此我有一个布尔变量,用于跟踪按钮是否已被单击:

I had a button, which when clicked showed the ad, so I had a boolean variable which tracked whether the button has been clicked:

boolean buttonClicked = false

这些行在我的onCreate函数中:

These lines were in my onCreate function:

    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(getContext());
    rewardedVideoAdListener = new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            if(buttonClicked) {
                showAd();
            }
        }

        @Override
        public void onRewardedVideoAdOpened() {

        }

        @Override
        public void onRewardedVideoStarted() {

        }

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

        @Override
        public void onRewarded(RewardItem rewardItem) {

        }

        @Override
        public void onRewardedVideoAdLeftApplication() {

        }

        @Override
        public void onRewardedVideoAdFailedToLoad(int i) {
            if(buttonClicked) {
                progressBar.setVisibility(View.INVISIBLE);
                Toast toast = Toast.makeText(getContext(), "Please try again later", Toast.LENGTH_SHORT);
                toast.show();
            }
        }

        @Override
        public void onRewardedVideoCompleted() {

        }
    };

    mRewardedVideoAd.setRewardedVideoAdListener(rewardedVideoAdListener);

    loadRewardedVideoAd();

    pointsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showAd();
            }
        });

这是我的showAd函数:

This was my showAd function:

public void showAd(){
    if (mRewardedVideoAd.isLoaded()) {
        progressBar.setVisibility(View.GONE);
        mRewardedVideoAd.show();
        buttonClicked = false;
    } else {
        loadRewardedVideoAd();
        progressBar.setVisibility(View.VISIBLE);
        buttonClicked = true;
    }
}

这是如何工作的,当创建活动/片段时,该应用尝试通过调用 loadRewaredVideoAd()函数在后台加载广告.然后,当用户单击按钮时,会调用 showAd()函数,并且发生以下两种情况之一:

How this works is, the app tries to load the ad in the background by calling the loadRewaredVideoAd() function when the activity/fragment is created. Then when the user clicks the button,showAd() function is called and one of two things happen:

  • 1)如果广告已成功加载,则会显示该广告.
  • 2)如果不是,它将再次调用 loadRewardedVideoAd()并显示一次进度条.还将 buttonClicked 设置为true.然后,如果广告加载,则调用 onRewardedVideoAdLoaded()函数,该函数再次调用 showAd(),这一次发生了第一个选项.如果广告这次也没有加载,则会调用 onRewardedVideoAdFailedToLoad(int i),并显示一个吐司,提示用户稍后再试.
  • 1) If the ad was successfully loaded, it shows the ad.
  • 2) If not, it calls loadRewardedVideoAd() again and shows a progressbar this time. It also sets buttonClicked to true. Then if the ad loads, the onRewardedVideoAdLoaded() function is called which calls showAd() again and this time the 1st option happens. If the ad didn't load this time as well, then onRewardedVideoAdFailedToLoad(int i)is called and it shows a toast saying the user to try again later.

这篇关于加载奖励视频广告时显示进度栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆