Rewarded Videos doesn't load the first time but loads after - java

My App has a button to open reward videos >> when this button is pressed for the first time, it toasts "Check your Internet connection", when hit the second or third time, it shows the video with no problems
MobileAds.initialize(getActivity(), getString(R.string.VID_App_ID));
mRewardVideoAd = MobileAds.getRewardedVideoAdInstance(getActivity());
mRewardVideoAd.loadAd(getString(R.string.VID_App_Unit_ID), new AdRequest.Builder()
.addTestDevice(getString(R.string.TestDeviceID))
.build());
mRewardVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
Here are the methods used :
private void loadRewardedVideoAd() {
mRewardVideoAd.loadAd(getString(R.string.VID_App_Unit_ID), new AdRequest.Builder()
.addTestDevice(getString(R.string.TestDeviceID))
.build());
}
#OnClick(R.id.button_more_money)
public void more_money() {
if (mRewardVideoAd.isLoaded()) {
mRewardVideoAd.show();
} else
{
Toast.makeText(getActivity(), "Check your internet connection", Toast.LENGTH_LONG).show();
loadRewardedVideoAd();
}
}
#Override
public void onResume() {
mRewardVideoAd.resume(getActivity());
super.onResume();
loadRewardedVideoAd();
}
Edit: Solved
- It took some time and the solution was to load at onCreate()
Thanks to Martin De Simone and Avi Levin

Rewarded videos take time to load, your code is fine, the first time you are pressing, the video is loading and then when you press probably the video has already loaded.
Try to toast something in the onAdLoaded to check this

As Martin said, RV ad takes a time to load. basically, it needs to download ~30-second video which takes sometimes few second.
I recommend using the SDK provided RewardedVideoAdListener interface which will help you know when the ad is ready to show. furthermore, it will help you to understand the AdMob rewarded video ad's life cycle.
In order to use it you need to do the following phases:
Implement RewardedVideoAdListener listener in your java class
Override the following methods (I used Toast UI messages for visualization, it can be deleted)
Code:
#Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this, "onRewarded! currency: " + reward.getType() + " amount: " +
reward.getAmount(), Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication",
Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
BTW: before a show call, I recommend using isLoaded() method in order to if the AdMob is ready to show something, for example:
if (mAd.isLoaded()) {
mAd.show();
}
More information can be found inside Google AdMob doc's

Related

Android Admob rewardedAd not showing rewarded ad

I'm using an admob rewardedAd and everything works fine but sometimes it doesn't show a video. Sometimes it shows a static ad like if it was an intersitial ad. It wouldn't be a problem, but when that happens and the user closes the app, onUserEarnedReward is not fired. Is there a way to force it to always show a rewarded video ad?
Here's the code where I load and prepare the callbacks for the ad:
rewardedAd = new RewardedAd(this, rewardKey);
RewardedAdLoadCallback adLoadCallback = new RewardedAdLoadCallback() {
#Override
public void onRewardedAdLoaded() { }
#Override
public void onRewardedAdFailedToLoad(int errorCode) {
rewardedAd = new RewardedAd(getApplicationContext(), rewardKey);
rewardedAd.loadAd(new AdRequest.Builder().build(), this);
}
};
adCallback = new RewardedAdCallback() {
#Override
public void onRewardedAdOpened() { }
#Override
public void onRewardedAdClosed() {
rewardedAd = new RewardedAd(getApplicationContext(), rewardKey);
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
}
#Override
public void onUserEarnedReward(#NonNull RewardItem reward) {
/*
...
Give rewards to the user
...
*/
rewardedAd = new RewardedAd(getApplicationContext(), rewardKey);
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
}
#Override
public void onRewardedAdFailedToShow(int errorCode) {
Toast.makeText(getApplicationContext(), getString(R.string.ad_error), Toast.LENGTH_LONG).show();
}
};
rewardedAd.loadAd(new AdRequest.Builder().build(), adLoadCallback);
Then I show it like:
rewardedAd.show(this, adCallback);
from your post it seems the issue does not happen all of the time. try not creating so many new instances of rewardedAd from this code you are calling it three times one should be enough rewardedAd = new RewardedAd(getApplicationContext(), rewardKey);
According to the the documentation there is no such thing as a static rewarded ad:
AdMob rewarded ads may be click-to-download video ads with an end
card that appears when the video ends or interactive ads, such as
playable ads or surveys.
Are you sure it's not showing an interactive ad such as a survey, where the user must answer a question to get the reward?

How to implement Reward video with Startapp

Recently I'm facing issue related to Startapp platform for Android.
When I implemented the reward video it returns an error with:
FailledError execute Exception Error sendGetWithResponse code = [204]
Here is my function:
public void rewardAd() {
Log.i("TAG", "Test");
startAppAd.setVideoListener(new VideoListener() {
#Override
public void onVideoCompleted() {
Log.i("TAG", "Grant User");
}
});
startAppAd.loadAd(StartAppAd.AdMode.REWARDED_VIDEO, new AdEventListener() {
#Override
public void onReceiveAd(com.startapp.android.publish.adsCommon.Ad ad) {
startAppAd.showAd();
}
#Override
public void onFailedToReceiveAd(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "Failled"+ ad.getErrorMessage());
startAppAd.showAd(new AdDisplayListener() {
#Override
public void adHidden(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adHidden");
}
#Override
public void adDisplayed(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adDisplayed");
}
#Override
public void adClicked(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adClicked");
}
#Override
public void adNotDisplayed(com.startapp.android.publish.adsCommon.Ad ad) {
Log.i("TAG", "adNotDisplayed"+ ad.getErrorMessage());
}
});
//startAppAd.showAd(getApplicationContext());
}
});
}
Here is the logs that shows:
TAG: Test
TAG: FailledError execute Exception Error sendGetWithResponse code = [204]
TAG: adNotDisplayed
When I comment the function startAppAd.showAd(), and uncomment the last commented one startAppAd.showAd(), it's working fine.
This is the expected working scheme:
Try to load a Video Reward Ad => startAppAd.loadAd();
if failed (like my case) => onFailedToReceiveAd();
Try to show an Ad that I can get listeners of it => startAppAd.showAd()
Thank You very much
It's solved, the script itself is not bad and works fine, the problem was with Startapp they don't show a video ad because it doesn't exist any video to show.
Now if someone is facing the same problem i suggest to use a vpn on testing device with a country that startapp should have a video ad usually US.

Service not working correctly

I am trying to learn services and background threads that can update UI, here, this service is started by calling startService() method from MainActivity, the problem is: when the loop runs for a larger value of i, Toasts are not shown for all of the values (interestingly Toast made after the loop also not showing ), but this time I tested with 10/20 etc and working perfectly.
Here are codes, of MyService.java:
public class MyService extends IntentService {
Handler mhandler;
public MyService() {
super("Hello");
}
#Override
public void onCreate() {
Toast.makeText(this, "Service Starting!", Toast.LENGTH_SHORT).show();
super.onCreate();
mhandler = new Handler();
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
mhandler.post(new Runnable() {
#Override
public void run() {
int i;
for (i = 0; i < 100; i++) {
Toast.makeText(MyService.this, "i is: " + i, Toast.LENGTH_SHORT).show();
}
Toast.makeText(MyService.this, "finally i is: "+i, Toast.LENGTH_SHORT).show();
}
});
}
}
And request for some really helpful resources for very beginner on Android Services, Background Tasks, Broadcast receiver. I got developer.android.com (Not too.. helpful for beginner), www.vogella.com, etc. Thanks in advance.
I believe the reason you are only seeing the first few values of i is because android will not allow an application to spam the toast queue.
As mentioned by #David Wasser in the OP post's comments, Toast is not a reliable debugging tool.
Use the logcat instead:
Log.v("TAG", "Message")

Android - Getting data out of Startapp

I use Startapp as my ad network in my applications and I need to be able to handle when a user clicks on an ad. I could not find any documentation for this function, and I would rather use onPause() because other things might pause the activity. Help would be appreciated!
Try this:
YourStartAppAd.showAd(new AdDisplayListener() {
#Override
public void adHidden(Ad ad) {
}
#Override
public void adDisplayed(Ad ad) {
}
#Override
public void adClicked(Ad ad) {
}
});
https://github.com/StartApp-SDK/Documentation/wiki/android-advanced-usage

Admob Interstitial Ads showing in the middle of the game

I just made game like ironpants, I'm trying to put interstitial ads when the game over.
but the interstitial ads always show in the middle of the game. I think it'll be annoying for users. and I'm afraid if Google will banned my admob account.
The interstitial ads only show after 5x game over.
I only have onescreen MainGameView.java
here's the code to show admob interstitial ads when the game was over
the methods to initialize the interstitial ads
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initialiseAccelerometer();
// Create the interstitial
interstitial = new InterstitialAd(this, getResources().getString(R.string.InterstitialAd_unit_id));
}
public void openAd() {
runOnUiThread(new Runnable() {
public void run() {
// Create ad request
AdRequest adRequest = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(new AdListener() {
#Override
public void onReceiveAd(Ad arg0) {
if (interstitial.isReady()) {
interstitial.show();
}
}
#Override
public void onPresentScreen(Ad arg0) {
}
#Override
public void onLeaveApplication(Ad arg0) {
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
}
#Override
public void onDismissScreen(Ad arg0) {
}
});
}
});
}
and I call openAd() method when the game was over
public synchronized void GameOver() {
if (adounter >= this.getResources().getInteger(R.integer.add_shows_every_X_gameovers)) {
openAd();
adounter = 0;
}
adounter++;
}
thank you very much and sorry for my bad English.
Your problem is that you are showing the interstitial d as soon as it is received (ie in AdListener#onReceiveAd). Don't do this. It provides a poor user experience and will ensure you get low ratings.
Instead show your add at a natural break in your app.
Instead show your add at a natural break in your app.
Exactly, to be more precise it would be good user experience if you do the following:
- request for the ad in onCreate
- in GameOver call:
if (interstitial.isReady()) {
interstitial.show();
}

Categories

Resources