InterstitialAd should load after every exposure - android

I need to use InterstitialAd in my app. But the problem is that after first exposure, before showing I am creating ad, call loadAd method with request, and than show (calling show method)it in the right moment. Everything seems to work good, but when I try to call show method next time it doesn't work because of isLoaded returns false.
Is there any way to load ad only once.
EDIT
Thx for answer, but the main problem is that loadAd is network connection, but the google suggest not to perform internet tasks in main thread, so what the f..k it is loading ad from the internet in main ui thread.It causes lags in my app. They are contradicting themselves.

You'd have to reload the ad anytime it's closed. I define a single method "reloadInterstitial()" and invoke it within the onCreate() method. It works great. All you have to do is call displayInterstitial() whenever you wish you to show the ad:
public class MyActivityClass extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("*****");
reloadInterstitial();
// Create ad request
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
}
public void displayInterstitial(){
if(interstitial != null && interstitial.isLoaded())
interstitial.show();
}
private void reloadInterstitial(){
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("*****");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("****");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}
private void loadInterCallBacks(){
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("xxxxxxxxxxx");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("xxxxxx");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}

Related

Admob interstitial ads are not closing

I'm showing interstitial ad in my app and ads are loading and displaying perfectly but the issue is that ads are not closing when I click cross button on back button I have to quit my app to close them
I created a new project and checked the ads in new project they are working fine and closing on back button but in my main app ads are not closing once they displayed to user
this is my Admob class
public class Admob {
public static InterstitialAd mInterstitial; // Interstital
private static AdView mAdView; // banner
public static void createLoadInterstitial(final Context context, View view)
{
mInterstitial = new InterstitialAd(context);
mInterstitial.setAdUnitId(context.getResources().getString(
R.string.admob_showIntersitial_ad_unit_id));
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
showInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
// Called when an ad leaves the app (for example, to go to the
// browser).
super.onAdLeftApplication();
}
});
loadInterstitial();
}
public static void loadInterstitial() {
mInterstitial.loadAd(new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/1033173712
build());
}
public static void showInterstitial() {
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
public static void createLoadBanner(final Context context, View view) {
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/6300978111
build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
});
}
}
This is how I show them in my activity
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
Admob.createLoadInterstitial(this,null);
I have initialize this in Mainfest also
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
I saw same questions on stackoverflow but none of them is answered the solution so any help will be really appreciated
If you are using Webiew check if there is webview is paused on ad shown

facebook interstitialad does not display close button in android app

For some reason facebook interstitialad does not display close button in my android app. I am followed all the steps from: https://developers.facebook.com/docs/audience-network/android/interstitial
However, using back button it does close the ad correctly. But I need to have close button displayed, any help?
First image:
Second Image:
Following is code which is responsible for this AD:
public class MyActivity extends SherlockFragmentActivity implements
SearchView.OnSuggestionListener, InterstitialAdListener {
private InterstitialAd interstitialAd;
boolean adDismissed;
private void loadInterstitialAd() {
interstitialAd = new InterstitialAd(this, "my_id");
interstitialAd.setAdListener(this);
interstitialAd.loadAd();
}
#Override
public void onAdLoaded(Ad ad) {
// Ad is loaded and ready to be displayed
// You can now display the full screen add using this code:
interstitialAd.show();
}
#Override
public void onAdClicked(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onInterstitialDismissed(Ad arg0) {
// TODO Auto-generated method stub
adDismissed = true;
}
#Override
public void onInterstitialDisplayed(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onError(Ad ad, AdError error) {
// Ad failed to load
}
#Override
public void onStart() {
super.onStart();
if(!adDismissed)
loadInterstitialAd();
}
}

Integrating of Analytics doesn´t work

I try to integrate google Analytics to my Application but it doesn´t work.
This is my Activity:
public class InhaltsverzeichnisActivity extends Activity {
private SharedPreferencesManager prefs; //added
private InterstitialAd Interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inhaltsverzeichnis);
prefs = new SharedPreferencesManager(this); //get SharedPreferencesManager instance
int t = prefs.retrieveInt("theme", 0); //get stored theme, zero is default
ThemeUtils.setTheme(t); //Set the stored theme, will default to Black
Tracker t1 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t1.setScreenName("Inhaltsverzeichnis");
t1.send(new HitBuilders.AppViewBuilder().build());
Interstitial = new InterstitialAd(this);
Interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXX/XXXXXXXXXX");
AdRequest adRequest = new AdRequest.Builder().build();
Interstitial.loadAd(adRequest);
;
Interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
Interstitial.show();
}
});
}
protected void displayInterstitial() {
// TODO Auto-generated method stub
}
public void onDismissScreen() {
// TODO Auto-generated method stub
}
public void onFailedToReceiveAd() {
// TODO Auto-generated method stub
}
public void onLeaveApplication() {
// TODO Auto-generated method stub
}
public void onPresentScreen() {
// TODO Auto-generated method stub
}
#Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStart(this);
}
#Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStop(this);
}
If I put the OnStart and OnStop method over the Interstitial Ad , Analytics works, but the Interstitial Ad goes crazy and appears every second again,
hope anyone can help me to solve this problem thanks.

AdListener unimplement methods android

Since I couldn't find any documentation about this methods I couldn't find an answer.
What is the purpose of these methods?
I added this AdRequest as and my logcat went crazy. Is that wrong?
#Override
public void onDismissScreen(Ad arg0) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
These are the callback methods, using these methods you can check that what is the status of the ad.
For example if the ad is not recived, onFailedToReceiveAd() will be called.

how to call ad mob ads show in some time intervals or different Activity in android apps

I am use ads of ad mob for advertise in my application its always display on on start up of my application but I want display this ads on when user exit application and back to the main activity call so how it implement in my app
my code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial = new InterstitialAd(this, "a152eb628a493d8");
adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
web=(WebView)findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://dcs-dof.gujarat.gov.in/live-info.htm");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,Menu.NONE,"About");
menu.add(0,2,Menu.NONE,"Feedback");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id == 1)
{
Toast.makeText(MainActivity2.this,"About",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity2.this,about.class);
startActivity(i);
}
else {
Toast.makeText(MainActivity2.this,"Feedback",Toast.LENGTH_LONG).show();
Intent i2 =new Intent(MainActivity2.this,feedback.class);
startActivity(i2);
}
return super.onOptionsItemSelected(item);
}
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
//interstitial.loadAd(adRequest);
super.onResume();
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this,"Press Again to Exit", Toast.LENGTH_SHORT).show();
}
#Override
public void onDismissScreen(Ad ad) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onRestart();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onStart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onDestroy();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onStop();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onPause();
}
You really don't want to attempt to display an ad to a user on exit from your Actvity, it is a poor user experience.
Even if you did orchestrate a way to get the ad and present prior to your Activity's destruction you don't know how long it will take to retrieve the ad so your Activity might hang for several seconds on exit. User's hate that kind of thing.
I strongly suggest you find another spot within your app in which to display ads.

Categories

Resources