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..
Related
Yesterday I created an interstitial ad using Admob in my Android app.
The test is showing fine, but if I put the real AdUnitId , the ad is not loading.
This is my code
MobileAds.initialize(this, "ca-app-pub-***");
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("ca-app-pub-**");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
Log.i("Ads", "onAdLoaded");
mInterstitialAd.show();
}
});
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.
What's the behavior of the admob interstitial after an error occurs? It automatically retries to reload the data or not?
In other words I have to write something like:
mInterstitialAd.setAdListener(new AdListener(){
//ERROR MANAGEMENT
#Override
public void onAdFailedToLoad(int error) {
mInterstitialAd.loadAd(adRequest);//Retry
}
});
Or admob auto-retry to load ad?
in case of error loading interstitial ad, try this code in your adListener's onAdFailedToLoad method :
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
this will create a new request for ad.
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.
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();
}
}