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?
Related
I am developing an Android app that uses the Google speech recognition api. For some reason, when I start up the app (via USB debugging on my Android phone), starting the listening process works just fine, and the app works smoothly if I do actually say something after clicking the button to make the app start listening for audio. However, if I click the button to start listening, and then I don't say anything, then the app crashes for some reason. In other words, if the speech recognition gets no results, then the app just crashes. Does anyone have any idea as to why?
Below is an excerpt of my code. (Yes, I did account for the permissions. I just didn't include that code in the excerpt below).
I have tried putting try catch blocks in various places to see if I could catch the error that causes the crash, but it has yet to work. Furthermore, while there is an onResults method, there does not seem to be an onNoResults method.
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
recognitionListener = new RecognitionListener() {
#Override
public void onReadyForSpeech(Bundle params) {
// Called when the speech recognition process is ready to begin
}
#Override
public void onBeginningOfSpeech() {
// Called when the user begins speaking
}
#Override
public void onRmsChanged(float rmsdB) {
// Called when the volume of the spoken input changes
}
#Override
public void onBufferReceived(byte[] buffer) {
// Called when the app receives a buffer of audio data
}
#Override
public void onEndOfSpeech() {
// Called when the user stops speaking
}
#Override
public void onError(int error) {
// Called when an error occurs during the speech recognition process
TextView textView = findViewById(R.id.errors);
textView.setText(error);
}
#Override
public void onResults(Bundle results) {
// Called when the speech recognition process has completed and the results are available
List<String> resultList = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
if (resultList != null && resultList.size() > 0) {
// Display the first result in the text field
TextView textView = findViewById(R.id.spokenWords);
textView.setText(resultList.get(0));
}
//speechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
#Override
public void onPartialResults(Bundle partialResults) {
// Called when the app receives partial results of the speech recognition process
}
#Override
public void onEvent(int eventType, Bundle params) {
// Called when the app receives other events from the speech recognition process
}
};
speechRecognizer.setRecognitionListener(recognitionListener);
Button startSpeech = findViewById(R.id.startSpeech);
startSpeech.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView textView = findViewById(R.id.spokenWords);
textView.setText("Listening");
speechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
}
});
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.
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
I've got a simple Android Wear demo to illustrate the Message API. I'm capturing input on a watch and passing it off to a handheld device for cloud processing, so the following class that extends WearableListenerService is running on a phone:
public class ListenerService extends WearableListenerService {
private static final String MESSAGE_PATH = "/handle-inbound-message";
#Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if(messageEvent.getPath().equals(MESSAGE_PATH)) {
updateData(new String(messageEvent.getData()));
}
}
private void updateData(final String volume) {
new Thread(new Runnable() {
#Override
public void run() {
// do neat stuff with the inbound data
}
}).start();
}
}
...and the wearable code generating the message, by way of clicking a button on the wearable app, is like so:
private void sendToHandheld(final byte[] volume) {
if(nodeId != null) {
new Thread(new Runnable() {
#Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
Wearable.MessageApi.sendMessage(client, nodeId, MESSAGE_PATH, volume);
client.disconnect();
}
}).start();
}
}
The code works fine...except for the fact that I have to click the button twice in the wearable app to get onMessageReceived() to fire. The app starts up, but doesn't get the message the first time...with the app still open, I click the button again and it fires perfectly. Can anyone see where I might have made a mistake?
(Also, I'm seeing a lot of code demos a repos where people don't call super.onMessageReceived().)
OK, I think I solved the issue - I commented-out the line in onMessageReceived() where the superclass is called...and that fixed it.
SUCCESS!
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();
}