public void adShow() {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-581420244534656/2222222");
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
requestNewInterstitial();
reseter();
}
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
reseter();
}
});
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("3D9834hiqewuiry48937498urequE")
.build();
mInterstitialAd.loadAd(adRequest);
}
}
I have tested the use of interstitial in my app using the default ad from google and it's working but now what must i do to make sure that the default orange ad is not displayed to users when I publish my app? I already have the AdUnitId.
So long as you have set a valid Ad unit id from the ad provider you're using, you should get real ads. The reason you most likely aren't seeing real ads is because of the line .addTestDevice("3D9834hiqewuiry48937498urequE") which basically says "Only show me the default orange ad on this device".
So, if you always test your app on the same device, this line is disabling real ads from appearing while you test (which is what you want). But this line will only disable real ads on a single device, the one you're using. On other devices, users will see real ads.
Related
In my application, I have integrated ads from AdMob. When I use my test unit ID, ca-
app-pub-3940256099942544/1033173712, it shows test ads successfully. However, when I use my production ad unit ID, it shows nothing.
In logcat, I see ads loading error code 0. Why are my ads failing to load?
This is the code I am using:
MobileAds.initialize(activity, main_interstial_addunit_id);
final InterstitialAd interstitialAd = new InterstitialAd(activity);
interstitialAd.setAdUnitId(main_interstial_addunit_id);
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
#Override
public void onAdClosed() {
}
#Override
public void onAdFailedToLoad(int i) {
super.onAdFailedToLoad(i);
}
#Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
}
#Override
public void onAdOpened() {
super.onAdOpened();
}
});
return interstitialAd;
So, as you are getting error code 0 this means "failed to load ads"
From this conversation,
It could be that you have only recently created a new Ad Unit ID and requesting for live ads. It could take a few hours for ads to start getting served if that is that case. If you are receiving test ads then your implementation is fine. Just wait a few hours and see if you are able to receive live ads then. If not, can send us your Ad Unit ID for us to look into.
So, after some times you will be automatically get ads.
Important: Ad type of your ad unit id needs to be same of ad type where you set that id
As we know, google requires us to use test device and test ad unit id, when we develop the app. However, I want to know that if there exists anyway that I can
see the real ad, because I am afraid that no ad will show after I change the code and ad id before release. I have successfully seen test ad, and then I changed the code and ad id, and then submit my app to beta testing, but the tester said that no ad was shown, is it normal, or I have made some mistake in my code or ad unit id.
Thank you for your help!
Below is my ad-related code, and I have changed the ad id
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
You can use actual ad unit id even for development, but your test devices should be added to the list of TEST DEVICES when you build your ad request.
If it's a valid ad unit id that you are using, you can be sure that ads will appear when you publish your apps. Also, you can check if you are getting hits for your ad unit in Adsense Dashboard.
If you want to be dead sure, you can just try to install the apk on another device, test it, and then publish it when you see the ads.
You can use AdListener and monitor why it is not showing ads . Remember if you have created fresh ad units then it will take some time or few hours to arrange live ads for it . If test ads are showing and you have valid ad unit id and also your app has not violated any policy then you are good to go (You will be emailed if your app have a policy issue) . Ads will be shown when availabe.
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest1 = new AdRequest.Builder().build();
adView.loadAd(adRequest1);
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
}
#Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
switch (errorCode){
case AdRequest.ERROR_CODE_INTERNAL_ERROR:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_INTERNAL_ERROR",Toast.LENGTH_SHORT).show();
break;
case AdRequest.ERROR_CODE_INVALID_REQUEST:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_INVALID_REQUEST",Toast.LENGTH_SHORT).show();
break;
case AdRequest.ERROR_CODE_NETWORK_ERROR:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_NETWORK_ERROR",Toast.LENGTH_SHORT).show();
break;
case AdRequest.ERROR_CODE_NO_FILL:
Toast.makeText(PlayListsActivity.this,"onAdFailedToLoad banner ERROR_CODE_NO_FILL",Toast.LENGTH_SHORT).show();
break;
}
Log.i("Ads", "onAdFailedToLoad");
}
#Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
Log.i("Ads", "onAdOpened");
}
#Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
Log.i("Ads", "onAdLeftApplication");
}
#Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
Log.i("Ads", "onAdClosed");
}
});
My doubt is not weather its ok to click on live ads during development.I did all the formalities to get my admob ad working . Then i saw that we should only use test add during development . so i used this code:
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") // An example device ID
.build();
It worked fine. But then i change i back to the live ad code fearing that i would forget to do so later:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
But now the ad wont appear during test run. Is that normal?
will the ad work when i finally launch the app?
I dont see anything wrong with your code, but something similar happened to me when implementing admob too.
I didnt have in count that the ad must be loaded before showing it, and that the loading may take some time. If you try to show an ad that its not loaded, a black activity might be shown.
You should implement some waiting time or a listener in order to be sure that the add is loaded before showing it.
public InterstitialAd interstitialAd;
[...]
public void setNewInterstitialRequest()
{
interstitialAd = new InterstitialAd(CurrActivity.this);
interstitialAd.setAdUnitId("ca-app-pub-***************************");
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice("ZY22247DJV")
.build();
// Begin loading your interstitial.
interstitialAd.loadAd(adRequest);
//add listener so you know is fully loaded
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
[...] // <- code you want to execute as soon as the ad is loaded
}
});
}
public void displayAd() {
try {
//verify is loaded before showing a black screen
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
//else
// showToast("Not loaded");
}
catch (Exception ex){}
}
So taking this in mind, if you want for the ad to be shown inmediately when something happens, you should have it loaded before that event occurs.
My banner adview showing correctly on real device and emulator. But interstilialad now showing real device although it showing correctly on emulator.
Every thing is okey for me. But i didn't understand why not showing.
EzFullScreenAds.class
public class EzFullScreenAds {
InterstitialAd mInterstitialAd;
Context context;
public EzFullScreenAds(Context context) { //constructor
this.context = context;
mInterstitialAd = new InterstitialAd(context);
mInterstitialAd.setAdUnitId(context.getString(R.string.adv_fullscreen));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
if (mInterstitialAd.isLoaded()) mInterstitialAd.show();
}
});
}
}
That is usage it.
new EzFullScreenAds(MainActivity.this);
May be because it cannot fill.
To show test ad on real device you must add real test device ID. AdRequest.DEVICE_ID_EMULATOR only work on emulator.
The Mobile Ads SDK uses the tag "Ads" when writing to logcat. You can
filter for that tag in Android Studio's logcat viewer, which makes it
easier to find your device's ID.
More information here:
https://firebase.google.com/docs/admob/android/targeting#adrequest
You did not add your device number as a test device.
The way you this is:
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("A1124HIDFBIUBQWASFDSAFD") // This is what is missing
.build();
The way how you can find your number is once you have the admob SDK installed(as you already have) You run the emulater and inside the log (that you can find in your working enviroment) press "ctrl + f" and search for "device".
Good Luck
Is it possible to load ads of Ad mob in starting of app and then use it in any button of whole project?
After click of any button it open quickly on every type of internet mainly on 2g
without taking time to load
it not a recommended way to show ads at start of the application as it result in poor user experience, like user might be interacting with your app and suddenly Ad pops up.
well if you still want to you can display it by overriding onAdLoad listener in your Activity's onCreate
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitialAd_id));
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
UPDATE:
With this method it will only display it if its loaded
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}