Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

android - How to know if AdMob ad has been loaded

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...