My question is that Admob Interstitial ad is displaying in testing mode but if app is released then ad is not displayed.
like i use
AdRequest mAdRequest;
mAdRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
`enter code here`.addTestDevice("12312132654654232").build();
but at the time releasing app i used code like this-
AdRequest mAdRequest;
mAdRequest = new AdRequest.Builder().build();
enter code here
this line not show ad.
Did you use these line in your code
first...
private InterstitialAd interstitial;
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(MY_AD_UNIT_ID);
then use this lines of code
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
and whenever you want to show add call this method
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
hope this will help you.
Related
I have a project where i am integrating Admob BANNER, INSTERSTITIAL and Native ads. I can integrate INTERSTITIAL and BANNER. but i dont know how to integrate NATIVE ADS.
I am working in eclipse
below is how i am integrating interstitial and banner.
final InterstitialAd interstitial;
//(((((((((( ADMOB Banner Ads )))))))
AdView adView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
interstitial = new InterstitialAd(activity);
interstitial.setAdUnitId("ca-app-pub-5900269475646349/1619195915");
//AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener()
{
// Listen for when user closes ad
public void onAdClosed()
{
//Load Interesterial add
// AdRequest adRequest = new AdRequest.Builder()
// .build();
// interstitial.loadAd(adRequest);
}
public void onAdLoaded()
{
if (interstitial.isLoaded()) {
interstitial.show();
}
}
});
To add native ads you have to use the latest googleplayservices lib.
To use it you have to add all projects in
Sdk\extras\google\m2repository\com\google\android\gms\ as projects.
I mean, extract , e.g. , play-services-10.2.0.aar as archive and add it into eclipse as library project. Do it with Sdk\extras\google\m2repository\com\google\android\gms\play-services-ads-lite\10.2.0\ too.
I try to add an Interstitial ads, the problem is that i have 2 code to change.
The 2 codes to change in my MainActivity:
But in my admob account, I can only find the ca-app-pub-xxxxx....xx code and I can't find the ID_INTERSTITIAL_UNIT_ID.
Can any body please show me where to find the second one?
I tried to put the same links in the two code but I am afraid of being banned from admob.
You will need only <string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/1033173712</string>
and do this on you onCreate() method
//region Interstitial
String interstitialAdId=getResources().getString(R.string.interstitial_ad_unit_id);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(interstitialAdId);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed()
{
}
});
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
//endregion
then add
if (mInterstitialAd.isLoaded())
mInterstitialAd.show();
where you want to start the Interstitial
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.
I am working with frame animations and i implement banner and interstitial ads in my activity, but when i do that it slows my application.
Here is my code:
private InterstitialAd interstitial;
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("UNIT ID");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
Any solutions on how to efficiently integrate admob in my class.
Answere to your comment
can i put all my admob code in someother class and initialize it in my
Main Activity?
Yes, For example: AdMob.java class:
import android.content.Context;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class AdMob {
private InterstitialAd interstitial;
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
void LoadInterstitialAd(Context ctx){
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(ctx);
// Insert the Ad Unit ID
interstitial.setAdUnitId("unit_id");
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
//.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
}
}
You can use it anywhere by doing this:
AdMob adMob = new AdMob();
adMob.LoadInterstitialAd(getApplicationContext());
I have only showed for interstitial ad, but it can be done for other ads too..
i wanted to call the Admob interstitials? multiple time in my Android app. i am tottally confusing this things.
interstitial = new InterstitialAd(this, "ID");
final AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
i am tried this code this works fine for one time call. when it will call second time show me error.
help me out this.
thanks in advance.
You need to separate out construction of the Interstitial, loading the ad and showing the ad.
public void onCreate(Bundle savedInstanceState) {
interstitial = new InterstitialAd(this, "MY-AD-ID");
interstitial.setAdListener(new AdListsner() {
..
});
}
private void loadAd() {
final AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
private void showAd() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}