Show a progress Bar when Rewarded video Ads is loading - java

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

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.

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

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.

Related

Huawei Ads, onAdClosed is not called

I'm trying to integrate the interstitial ad from Huawei Ads, when I click on (X) or back button to close the ad, for some reason the Toast inside onAdClosed method is not showing.
huaweiInterstitialAd = new InterstitialAd(MainActivity.this);
huaweiInterstitialAd.setAdId("teste9ih9j0rc3");
private void showHuaweiInterstitial() {
if (huaweiInterstitialAd != null && huaweiInterstitialAd.isLoaded()) {
huaweiInterstitialAd.show(MainActivity.this);
}
}
public void loadHuaweiInterstitialAd() {
huaweiInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
showHuaweiInterstitial();
}
#Override
public void onAdFailed(int errorCode) {
Log.d(TAG, "Ad load failed with error code: " + errorCode);
}
#Override
public void onAdLeave() {
super.onAdLeave();
Log.d(TAG, "onAdLeave");
}
#Override
public void onAdClosed() {
super.onAdClosed();
Toast.makeText(MainActivity.this, "Ad Closed", Toast.LENGTH_SHORT).show();
}
#Override
public void onAdClicked() {
Log.d(TAG, "onAdClicked");
super.onAdClicked();
}
#Override
public void onAdOpened() {
Log.d(TAG, "onAdOpened");
super.onAdOpened();
}
});
AdParam adParam = new AdParam.Builder().build();
huaweiInterstitialAd.loadAd(adParam);
}
The problem only occurs if you use:
implementation 'com.huawei.hms:ads:3.4.46.300'
But everything works fine with :
implementation 'com.huawei.hms:ads-lite:13.4.46.300'

How do I implement interstitial ads to show after X amount of times a player switches to the gameover screens?

So, I am new to AdMob and I am trying to figure out how to display an interstitial ad after the player dies and goes to the game over screen X amount of times. I have my AdMob set up in my AndroidLauncher class, however, my other classes do not have the ad variables. this is what my AndroidLauncher class looks like currently. If it helps, my game is set up with game states, 0 is before game starts, 1 is currently playing, and 2 and 3 are both game over states that send the player to the game over screen.
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
ad = new InterstitialAd(this);
ad.setAdUnitId("ca-app-pub-2188258702xxxxxxxxxxx");
ad.loadAd(new AdRequest.Builder().build());
ad.setAdListener(new AdListener(){
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
if (ad.isLoaded()) {
ad.show();
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
#Override
public void onAdOpened() {
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
#Override
public void onAdClosed() {
}
}); ```
I am not sure if this is the best way but I can explain my way of doing it. This may help.
1) Create an Interface for Adservice
public interface AdService {
void showOrLoadInterstital();
void showBanner();
void hideBanner();
void showRewardedAd();
boolean hasVideoLoaded();
boolean checkAdWatched();
}
2) Implement this interface from your AndroidLauncher class and have a constructor of your base Game Class with this ad interface. From your androidlauncher class, refer to this constructor. Also generate the interface classes. ( I am only posting relevant parts).
public class AndroidLauncher extends AndroidApplication implements AdService {
#Override
protected void onCreate(Bundle savedInstanceState) {
// rest of the code here
setUpInterstitial();
View gameView = initializeForView(new MainGameClass(this), config);
}
private void setUpInterstitial() {
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
loadIntersitialAd();
}
private void loadIntersitialAd() {
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest interstitialRequest = builder.build();
interstitialAd.loadAd(interstitialRequest);
}
#Override
public void showOrLoadInterstital() {
runOnUiThread(new Runnable() {
public void run() {
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
loadIntersitialAd();
}
#Override
public void onAdFailedToLoad(int i) {
loadIntersitialAd();
}
});
interstitialAd.show();
adWatched=false;
}
});
}
}
3) Call the ad when you want to show it.
if (MainGameClass.adService != null) {
RunnableAction playWooshAction = Actions.run(new Runnable() {
#Override
public void run() {
MainGameClass.adService.showOrLoadInterstital();
}
});
playWooshAction.run();
}
4) For showing ad after X amount of time, just set a timer.

How to resume download file using PRDownloader in android?

i have a recycler view and i want to control of downloading using buttons in my card view.
i use PRDownloader library but i have issue i can't resume download after finish the app and open it again it start the download not resume it.
my code as below
holder.downloadResumeOrPlayBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
downLoadID = PRDownloader.download(remoteLink, myLocalDirectory,
fileName)
.build()
.setOnStartOrResumeListener(new OnStartOrResumeListener() {
#Override
public void onStartOrResume() {
holder.pauseDownloadBtn.setVisibility(View.VISIBLE);
holder.cancelBtn.setVisibility(View.VISIBLE);
holder.downloadResumeOrPlayBtn.setVisibility(View.GONE);
notifyDataSetChanged();
}
})
.setOnPauseListener(new OnPauseListener() {
#Override
public void onPause() {
holder.pauseDownloadBtn.setText(mContext.getResources().getString(string.resume_text_btn));
holder.downloadResumeOrPlayBtn.setVisibility(View.GONE);
holder.pauseDownloadBtn.setVisibility(View.VISIBLE);
holder.cancelBtn.setVisibility(View.GONE);
notifyDataSetChanged();
}
})
.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel() {
holder.mProgressBar.setProgress(0);
holder.downloadResumeOrPlayBtn.setText(mContext.getResources().getString(R.string.download_text));
holder.downloadResumeOrPlayBtn.setVisibility(View.VISIBLE);
holder.pauseDownloadBtn.setVisibility(View.GONE);
holder.cancelBtn.setVisibility(View.GONE);
notifyDataSetChanged();
}
})
.setOnProgressListener(new OnProgressListener() {
#Override
public void onProgress(Progress progress) {
video.setVideoProgress(progress.currentBytes * 100 / progress.totalBytes);
Log.i("videosAdapter", video.getVideoProgress() + "");
holder.mProgressBar.setProgress((int) video.getVideoProgress());
holder.mProgressBar.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
})
.start(new OnDownloadListener() {
#Override
public void onDownloadComplete() {
System.out.println("download completed");
holder.downloadResumeOrPlayBtn.setText(mContext.getResources().getString(string.play_video_text));
holder.downloadResumeOrPlayBtn.setVisibility(View.VISIBLE);
holder.pauseDownloadBtn.setVisibility(View.GONE);
holder.cancelBtn.setVisibility(View.GONE);
notifyDataSetChanged();
}
#Override
public void onError(Error error) {
holder.downloadResumeOrPlayBtn.setText(mContext.getResources().getString(string.download_text));
Toast.makeText(mContext, error.getServerErrorMessage(), Toast.LENGTH_SHORT).show();
holder.mProgressBar.setProgress(0);
}
});
}
if(holder.downloadResumeOrPlayBtn.getText().toString().equals(mContext.getResources().getString(string.resume_text_btn)))
{
PRDownloader.resume(downLoadID);
}
});
my question is how to resume the download without downloading the file from the start and if there is another library to use how can i make it.

How to wait for a AdMob Rewarded Video to Load properly

I am trying to use the AdMob Rewarded Ad in my Android application.
In order to include it in the refered app, I need to wait for the rewarded video to load after the user click the buttom.
I am trying it throughout the code bellow, but I get the error:
java.lang.IllegalStateException: showAd must be called on the main UI
thread.
When the buttom is clicked:
b_r_ans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isSomBotaoLigado() && loaded[0])
soundPool.play(soundID, MyApplication.getVolumeBotao(), MyApplication.getVolumeBotao(), 1, 0, 1f);
Toast.makeText(MyApplication.getAppContext(),carregando_rv, Toast.LENGTH_LONG).show();
Thread t_rv = new Thread(new Runnable() {
public void run() {
while(!rv_loaded){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t_rv.start();
try {
t_rv.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
mRewardedVideoAd.show();
}
});
Overrided the listener:
#Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
rv_loaded = true;
}
Using the mRewardedVideoAd.isLoaded() function triggers the same issue.
How can I wait for the video to load properly?
It appears to happen that you are calling show from a different thread to the UI so I'd try to force its execution on the main thread like:
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = new Runnable() {
#Override
public void run() {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
};
mainHandler.post(myRunnable);
You can also disable the button until the ad is loaded, then in the onRewardedVideoAdLoaded function enable the button:
override fun onRewardedVideoAdLoaded() {
ad_btn.isEnabled = true
}
Hope it helps. This is the implementation that I am using.
Solved it through this code:
#Override
public void onRewardedVideoAdLoaded() {
//Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
rv_loaded = true;
if(goToAnswers)
mRewardedVideoAd.show();
goToAnswers = false;
}
b_r_ans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isSomBotaoLigado() && loaded[0])
soundPool.play(soundID, MyApplication.getVolumeBotao(), MyApplication.getVolumeBotao(), 1, 0, 1f);
Toast.makeText(MyApplication.getAppContext(),carregando_rv, Toast.LENGTH_LONG).show();
if(mRewardedVideoAd.isLoaded())
mRewardedVideoAd.show();
else goToAnswers=true;
}
});
This code will show a ProgressDialog while loading the reward video:
package com.mountzoft.waitForrewardvideoProperly;
import android.app.ProgressDialog;
import android.content.Context;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mAd;
private ProgressDialog mProgressDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Please wait... Loading Reward Video Ad!");
mProgressDialog.setCancelable(false);
}
private final showRewardVideoAd() {
mProgressDialog.show();
initializeRewardVideoAd();
loadRewardedVideoAd();
}
private void initializeRewardVideoAd(){
String adMobAppId = BuildConfig.AD_MOB_APP_ID; //id is stored in build gradle file
MobileAds.initialize(this, adMobAppId);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
}
private void loadRewardedVideoAd() {
String admobAdUnitId = BuildConfig.ADMOB_AD_UNIT_ID;//id is stored in build gradle
mAd.loadAd(admobAdUnitId,
new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {
mAd.show();
}
#Override
public void onRewardedVideoAdOpened() {
mAd = null;
}
#Override
public void onRewardedVideoStarted() {
mProgressDialog.dismiss();
}
#Override
public void onRewardedVideoAdClosed() {
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
#Override
public void onRewardedVideoCompleted(){
}
}
If you need to wait until rewarded ad loading use SingleLiveEvent. Please refer following code snippet.
private val isRewardedAdLoaded: LiveData<Event<Boolean>>
get() = _isRewardedAdLoaded
private var _isRewardedAdLoaded: MutableLiveData<Event<Boolean>> = MutableLiveData()
I used this is in fragment. So this from onViewCreated()
rewardedAd = createAndLoadRewardedAd()
and I post loading state as following
private fun createAndLoadRewardedAd(): RewardedAd {
val rewarded = RewardedAd(requireContext(), getString(R.string.admob_rewarded_id_test))
val adLoadCallback = object : RewardedAdLoadCallback() {
override fun onRewardedAdLoaded() {
Log.e(TAG, "onRewardedAdLoaded")
_isRewardedAdLoaded.postValue(Event(true))
}
override fun onRewardedAdFailedToLoad(errorCode: Int) {
_isRewardedAdLoaded.postValue(Event(false))
}
}
rewarded.loadAd(Builder().build(), adLoadCallback)
return rewarded
}
this is rewarded ad show method
private fun showRewardedAds(rewarded: RewardedAd) {
rewarded.show(requireActivity(), object : RewardedAdCallback() {
override fun onRewardedAdOpened() {
Log.e(TAG, "onRewardedAdOpened")
}
override fun onRewardedAdClosed() {
Log.e(TAG, "onRewardedAdClosed")
rewardedAd = createAndLoadRewardedAd()
}
override fun onUserEarnedReward(rewardItem: RewardItem) {
Log.e(TAG, "onUserEarnedReward")
translateText()
}
override fun onRewardedAdFailedToShow(errorCode: Int) {
translateText()
}
})
}
Finally I called as following
R.id.btnTranslate -> {
if (rewardedAd.isLoaded){
showMessage(
getString(R.string.translate_ads_show_warning_title),
getString(R.string.translate_ads_show_warning_msg)
)
} else {
progress.setVisible(true)
isRewardedAdLoaded.observe(viewLifecycleOwner, Observer {
it.getContentIfNotHandled()?.let { loaded ->
if (loaded and progress.isVisible){
progress.setVisible(false)
showMessage(
getString(R.string.translate_ads_show_warning_title),
getString(R.string.translate_ads_show_warning_msg)
)
}
}
})
}
}

Android move from one activity to another

I was having some problem for Android activity transition. Basically what I am trying to do is share text to Twitter. However, when I open up the twitter content, it took quite a few seconds to load up the content and resulting in the white blank activity for a few seconds.
And here is my codes, when my button onClick, I am executing the loading dialog:
ivTwitterShare.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Thread newThread = new Thread() {
#Override
public void run() {
try {
super.run();
sleep(10000);
} catch (Exception e) {
} finally {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(tweetUrl));
startActivity(intent);
progressDialog.dismiss();
}
}
};
newThread.start();
new LoadTwitterTask().execute();
}
});
private class LoadTwitterTask extends AsyncTask<Void, Integer, Void> {
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Loading Twitter...",
"Retrieving Twitter information, please wait...", false,
false);
EventDialogueBox.customizeDialogueBox(context, progressDialog);
}
#Override
protected Void doInBackground(Void... params) {
try {
synchronized (this) {
int counter = 0;
while (counter <= 4) {
this.wait(50);
counter++;
publishProgress(counter * 25);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
progressDialog.setProgress(values[0]);
}
#Override
protected void onPostExecute(Void result) {
}
}
However, my problem now is the white blank page before the content is loaded up still there. What I wanted is firstly, the loading dialog will show. Then, at the same time, the twitter intent is loading. Once finish loaded up the content, then dialog will be dismissed.
Any ideas?
Thanks in advance.

Categories