Show a progress Bar when Rewarded video Ads is loading

佐手、 提交于 2021-01-05 10:42:05

问题


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


回答1:


This is how I did it:

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

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

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

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



回答2:


add OnProgressUpdate() in your class that extends AsyncTask and add progress dialog in this.




回答3:


I am placing an admob video, which is executed when you enter the activity. In my case, I put an executable loop to start the video when it is already loaded.

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

This is part of all my code

private boolean started = false;
private Handler handler = new Handler();

private void initializeAdMob() {
    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    mRewardedVideoAd.setRewardedVideoAdListener(this);
    loadRewardedVideoAd();
}

private void loadRewardedVideoAd() {
    if (!mRewardedVideoAd.isLoaded()) {
        mRewardedVideoAd.loadAd(getString(R.string.id_block_activity), new AdRequest.Builder().build());
    }
}

private void showRewardedVideoAd() {
    if (BaseInteractor.isNetworkAvailable(this)) {
        showProgressBar(true);
        start();
    }
}

private Runnable runnable = new Runnable() {
    @Override
    public void run() {
        if (mRewardedVideoAd.isLoaded()) {
            mRewardedVideoAd.show();
        }else{
            loadRewardedVideoAd();
        }
        if (started) {
            start();
        }
    }
};

public void stop() {
    started = false;
    handler.removeCallbacks(runnable);
}

public void start() {
    started = true;
    handler.postDelayed(runnable, 2000);
}

finally I stop the runnable in

@Override
public void onRewardedVideoAdOpened() {
    showProgressBar(false);
    stop();
}

I hope this helps you.



来源:https://stackoverflow.com/questions/48325879/show-a-progress-bar-when-rewarded-video-ads-is-loading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!