Return to Video

Showing and Loading Interstitial Ad

  • 0:00 - 0:04
    All right, before we go into the code
    of displaying an Interstitial Ad,
  • 0:04 - 0:06
    let's discuss this a little bit.
  • 0:06 - 0:10
    Because there are two important things
    to think about with Interstitial Ads.
  • 0:10 - 0:14
    The first one, is about
    the format of an Interstitial Ad.
  • 0:14 - 0:18
    As we know, the Interstitial Ad consumes
    the entire screen of the device.
  • 0:18 - 0:21
    Therefore, it's important
    not to interrupt the user
  • 0:21 - 0:23
    while they are engaging with your app.
  • 0:23 - 0:28
    So the first important things to think
    about, is to display Interstitial Ads
  • 0:28 - 0:34
    with our natural breaks in your app,
    for example, between levels in a game.
  • 0:34 - 0:37
    The second important thing with
    Interstitial Ads is to prepare
  • 0:37 - 0:40
    the loading of the ad before showing it.
  • 0:40 - 0:42
    Why is this important?
  • 0:42 - 0:46
    Well, loading an Interstitial Ad
    can take time, in particular,
  • 0:46 - 0:47
    if the network is slow.
  • 0:47 - 0:52
    When the time has come to display an
    Interstitial Ad, you don't want the user
  • 0:52 - 0:56
    to sit there and wait for it to load,
    you want it to be displayed immediately.
  • 0:56 - 0:58
    Therefore, we load it in advance.
  • 0:58 - 1:02
    We prepare the loading
    before we're showing the ad.
  • 1:02 - 1:04
    So how do we do that.
  • 1:04 - 1:07
    Well, let's start by looking
    at the class Interstitial Ad.
  • 1:07 - 1:12
    This is the class we use to load and
    show an Interstitial Ad.
  • 1:12 - 1:14
    So it has a method called loadAd.
  • 1:14 - 1:18
    But this method is just requesting
    the ad to be loaded and
  • 1:18 - 1:20
    to avoid blocking the UI thread,
  • 1:20 - 1:24
    it will return immediately
    before the ad has been loaded.
  • 1:24 - 1:26
    So how do we know when
    the ad has been loaded?
  • 1:26 - 1:31
    By setting an AdListener,
    we can overload to onAdLoaded method.
  • 1:31 - 1:35
    So when the Interstitial Ad has finished
    loading, this method will be called, and
  • 1:35 - 1:39
    we can then proceed to call
    show on the Interstitial Ad.
  • 1:39 - 1:42
    So, this is the flow that we
    will implement in our app.
  • 1:42 - 1:46
    You should also know that Interstitial
    Ad has a method called isLoaded.
  • 1:46 - 1:50
    So if you're not interested in knowing
    exactly when the ad has been loaded,
  • 1:50 - 1:52
    then you could simply call loadAd.
  • 1:52 - 1:57
    And when the time has come to show the
    ad, you check if the ad has been loaded.
  • 1:57 - 2:00
    If it has been loaded,
    then you call the show method.
  • 2:00 - 2:02
    Otherwise, you do not
    show the ad at all.
  • 2:02 - 2:04
    Now let's check out the code
    to implement this flow.
  • 2:06 - 2:09
    Let's open up
    the InterstitialActivity Java file.
  • 2:09 - 2:14
    The first thing we've done here is to
    add an InterstitialAd field called
  • 2:14 - 2:19
    mInterstital, there are no
    changes to the onCreate method.
  • 2:19 - 2:21
    So let's go and
    look at the loadInsterstitial method.
  • 2:21 - 2:24
    This code should already be there
    because we set it in the previous
  • 2:24 - 2:25
    exercise.
  • 2:25 - 2:27
    So let's look at the other code.
  • 2:27 - 2:31
    First, we're creating an instance
    of the InterstatialAd.
  • 2:31 - 2:33
    Then, we're setting the AdUnitId.
  • 2:33 - 2:37
    And we set it to this string that
    will return test InterstitialAds.
  • 2:37 - 2:40
    And this string, we defined in our
    resource string file, so we have to get
  • 2:40 - 2:45
    it using the getResources().getString
    and then the resource ID.
  • 2:45 - 2:47
    And now comes the flow
    that we talked about.
  • 2:47 - 2:51
    Here, you can see that we're setting
    the listener for the InterstitialAd.
  • 2:51 - 2:53
    Let's talk about this in a little while.
  • 2:53 - 2:57
    But we're setting a listener here and
    then the execution drops down here.
  • 2:57 - 3:01
    So now, you can see, we're creating
    an AdRequest using the builder.
  • 3:01 - 3:06
    And then we're calling the loadAd
    method on our InterstitialAd instance.
  • 3:06 - 3:09
    Providing the AdRequest as an argument,
    and as we saw before,
  • 3:09 - 3:12
    this method will start
    the ad loading process, but
  • 3:12 - 3:16
    it will return immediately
    before the ad has been loaded.
  • 3:16 - 3:19
    That's where our listener comes
    into play, and as you can see,
  • 3:19 - 3:23
    we're creating an anonymous class here
    based on our ToastAdListener class.
  • 3:23 - 3:27
    And as you can see,
    we're overloading the onAdLoaded method.
  • 3:27 - 3:29
    So since we're setting
    the listener here,
  • 3:29 - 3:33
    this method will be called when
    the ad has completed loading.
  • 3:33 - 3:36
    So we call our parent method that
    will display the toast message,
  • 3:36 - 3:39
    since that is what we defined
    in the ToastAdListener class.
  • 3:41 - 3:45
    And then we set the text of our
    disable button to Show Interstitial.
  • 3:45 - 3:47
    And we also enabled that button.
  • 3:47 - 3:50
    That means that the user
    can now press this button.
  • 3:50 - 3:51
    And when that happens,
  • 3:51 - 3:55
    the showInterstitial callback
    method will be called.
  • 3:55 - 3:57
    So what are we doing
    is showInterstitial.
  • 3:57 - 4:01
    Well, with simple check if
    the Interstitial Ad has been loaded, and
  • 4:01 - 4:03
    this may seem like an unnecessary check,
  • 4:03 - 4:07
    since we know the ad has been
    loaded since onAdLoaded was called.
  • 4:07 - 4:09
    But we're doing it any how.
  • 4:09 - 4:12
    And then the time has come to actually
    show the Interstitial Ad, and
  • 4:12 - 4:16
    when this happens, the ad will actually
    be displayed in full screen mode.
  • 4:17 - 4:19
    And for the button,
    we go back to a previous state,
  • 4:19 - 4:23
    setting the text Interstitial Not Ready
    and then disabling it.
  • 4:23 - 4:26
    And finally, going back to our listener.
  • 4:26 - 4:30
    If the ad could not be loaded, the
    onAdFailedToLoad method will be called.
  • 4:30 - 4:34
    In that case, again,
    we show the toast message and
  • 4:34 - 4:37
    then we set the ErrorReason
    to the text of the button.
  • 4:37 - 4:41
    That's it, that's the code we have
    to implement the Interstitial Ad.
Cím:
Showing and Loading Interstitial Ad
Leírás:

more » « less
Video Language:
English
Team:
Udacity
Projekt:
ud876-3 - Google Play Services - AdMob
Duration:
04:42

English subtitles

Felülvizsgálatok Compare revisions