25

My Andorid app works offline and online. It displays ads when it is in online mode. In a scenario where it is working in offline mode and user switches the internet connectivity on, I want to know if the ad is already loaded. If not, then I would load a new ad. I looked at AdMob API (AdView class) but could not find something that does this.

Here is the implementation of my AdListener according to the answer from @Hounshell. But none of the methods implemented here are getting executed.

        adView.setAdListener(new AdListener() {

        @Override
        public void onReceiveAd(Ad arg0) {
            Toast.makeText(act, "Ad received",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onPresentScreen(Ad arg0) {
        }

        @Override
        public void onLeaveApplication(Ad arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
            Toast.makeText(act, "Failed to receive Ad",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onDismissScreen(Ad arg0) {
            // TODO Auto-generated method stub

        }
    });

And part of main.xml that covers the AdView

<FrameLayout 
                    android:layout_width="fill_parent"
                    android:layout_height="50dp"
                    >
                <com.google.ads.AdView
                    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                    android:id="@+id/adView"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    ads:adSize="BANNER"
                    ads:adUnitId="xxxxxxxxxxxxxxxx"
                    ads:loadAdOnCreate="true" />
                </FrameLayout>
Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35
  • In my personal experience, when I turned on the internet for the device an ad loaded. – AJcodez Mar 21 '12 at 01:34
  • Yes. I did expect it to work like that. I have updated my question with a part of main.xml. And I'm just creating new instance of AdView in onCreate() of the Activity. I suppose that should be enough. Could you post some code that you are using? – Balkrishna Rawool Mar 22 '12 at 08:09

5 Answers5

33

From https://developers.google.com/mobile-ads-sdk/docs/android/intermediate#adlistener

AdView.setAdListener(new AdListener() {
      // Implement AdListener
    });

Your AdListener's onReceiveAd() will be called when an ad is available, onFailedToReceiveAd() will be called whan an ad isn't available with a code explaining why (including network not available and no fill)

Update:

Same basic answer, new URL: https://developers.google.com/admob/android/banner?hl=en

Hounshell
  • 5,321
  • 4
  • 34
  • 51
  • 1
    I implemented an `AdListener` as you mentioned. But I can't see that any of these implemented methods are getting executed. Although I can see ads when the device is connected internet and when it is not connected to internet, the ads don't show up. – Balkrishna Rawool Mar 21 '12 at 21:50
  • Can you post the code that implements it and where you set it? – Hounshell Mar 21 '12 at 23:16
  • I implemented `AdListener` as mentioned in the answer from @Hounshell. I have will edit the question with this code. But the problem is none of the methods implemented in the `AdListener` are getting executed. – Balkrishna Rawool Mar 21 '12 at 23:19
  • Just to cover all of the bases, do you have breakpoints or some other statement in the onReceiveAd() method that you're using to check that it's called? System.out.println() doesn't actually do anything in Android. http://stackoverflow.com/questions/2220547/why-doesnt-system-out-println-work-in-android – Hounshell Mar 21 '12 at 23:25
  • Thanks for the response. I was not aware of the fact that `System.out.println()` doesn't do anything. But in fact I had also put an alert dialogue in the implementation of these methods and it did not show up also. I removed it while putting the code here and replaced it with `System.out.println()`. Now I have added `Toast.makeText()`. But it also doesn't show up. Above is my updated code. – Balkrishna Rawool Mar 21 '12 at 23:42
  • Can you post your LogCat logs? If you want to filter it to just AdMob you can do `adb logcat Ads:* *:S` – Hounshell Mar 22 '12 at 13:55
  • I tried two different scenarios: 1. Device is online.. Open app.. Make device offine.. Make device online.. 2. Device offline.. Open app.. Make device online.. Check ads. It works fine in scenario 1. But in scenario 2 the ads never show up even when the device gets online.. Here is the log: `03-22 22:07:14.882: I/Ads(722): AdLoader timed out after 60000ms while getting the URL. 03-22 22:07:14.906: I/Ads(722): onFailedToReceiveAd(A network error occurred.)` It looks like it doesn't check for internet accessibility once it times out and so even when the device is online there is no log. – Balkrishna Rawool Mar 22 '12 at 21:14
  • Another thing I just noticed. I tried the app on a real device instead of emulator. When the device is offline I open app and no ads.. then I start internet connectivity.. still no ads. But if I rotate the device, the view gets reloaded and ad shows up. @Hounshell – Balkrishna Rawool Mar 22 '12 at 23:05
  • 1
    Well.. I have searched a lot for this now and finally I have given up. I found a suitable event in my app and I now fire a new request for ad if the internet connectivity has been just established. Thanks @Hounshell for your help. – Balkrishna Rawool Mar 22 '12 at 23:26
  • Yeah, the AdView doesn't monitor internet connectivity. Once a request fails, it won't auto-request another ad. The approach you're taking is probably the best: register for the events yourself and manually re-request an ad (I assume you're using `loadAd()`) – Hounshell Mar 23 '12 at 04:01
  • Have you tried setting up adListener as an interface? I am running into the same issue, but I do receive logs in my logcat when ads are loaded. Look at my setup here: http://stackoverflow.com/questions/20862601/how-to-get-test-ad-banners-and-test-interstitial-ads-working-for-admob/20862719?noredirect=1#20862719 – portfoliobuilder Dec 31 '13 at 20:58
  • @Hounshell link is not available, please change. Thank you! – aB9 Sep 27 '15 at 18:59
  • @RuchirBaronia: Perhaps because the method setAdListener() is not static and you are calling it in static way. I'd say ask a new question and show your code. – Balkrishna Rawool Nov 16 '15 at 16:57
  • @BalkrishnaRawool So basically, I have no ads in my splash screen. Instead, I am trying to load the ads (which are in the **next** activity) while I am in the splash screen. The problem is, I have no adView in my splash screen to put `setAdListener` to, as the adView is in the next activity. How can I load the add from the next activity then? I don't have much code to provide, because I don't know which adView to add the listener to. I am sure this is a very simple issue to resolve, and I really appreciate your help. Thanks so much! – Ruchir Baronia Nov 16 '15 at 23:56
6

Simply...!!!

final AdView mAdView = (AdView) findViewById(R.id.adView);
        mAdView.setVisibility(View.GONE);
    mAdView.setAdListener(new AdListener() {
        private void showToast(String message) {
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdLoaded() {
            showToast("Ad loaded.");
            if (mAdView.getVisibility() == View.GONE) {
                mAdView.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            showToast(String.format("Ad failed to load with error code %d.", errorCode));
        }

        @Override
        public void onAdOpened() {
            showToast("Ad opened.");
        }

        @Override
        public void onAdClosed() {
            showToast("Ad closed.");
        }

        @Override
        public void onAdLeftApplication() {
            showToast("Ad left application.");
        }
    });


    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Waqar Vicky
  • 373
  • 1
  • 3
  • 12
6

I solved this problem with a bit of a workaround. For the first time, I checked if AdView has an empty tag or not.

if (adView.getTag() != null && adView.getTag() instanceof Boolean && (Boolean) adView.getTag()) {
        //Adview is already loaded
    }

If adView has a Boolean tag object and it is true then ad is already loaded otherwise load the ad and set the tag -

adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                adView.setTag(true); // Set tag true if adView is loaded
            }

            @Override
            public void onAdFailedToLoad(int i) {
                super.onAdFailedToLoad(i);
                adView.setTag(false); // Set tag false if loading failed
            }
        });
mihirjoshi
  • 12,161
  • 7
  • 47
  • 78
1

You can use the getResponseInfo() method to retrieving information about the loaded ad.

if (adView.getResponseInfo() == null) {
   adView.loadAd(adRequest);
}

https://developers.google.com/admob/android/response-info

Kai Burghardt
  • 1,493
  • 16
  • 16
-2

you can use isLoaded() method. I am using it on a same context as you and inside a timer to wait until ad is loaded.

https://developer.android.com/reference/com/google/android/gms/ads/InterstitialAd.html

Jaume
  • 3,672
  • 19
  • 60
  • 119