B4A Library Admob Interstitial Library

Because the old Admob SDK will be deprecated in August, I have written a library for interstitials using the new Google Play Services library. You'll need to follow initial steps noted in Erel's post for the Admob banner library to get your app ready for this library.

The new library should be compatible with your old code with the exception of a change to the AdFailedToReceive event. It should be declared in your app as follows.

B4X:
Sub mwadi_AdFailedToLoad (ErrorMessage As String)
    Log("failed to load ad: " & ErrorMessage)
End Sub

This new library also supports a new event that triggers when the user closes an ad. You can use this event to queue a new ad to load or run other code.

B4X:
Sub mwadi_AdClosed
    mwAdInterstitial.LoadAd
End Sub

The new library is called mwAdmobInterstitial2. I added the 2 to the end of the name so you can preserve your old library in case it is needed with other apps. The new library and example app for the library is attached at the end of this post.

============================================
Below is for the old version (not using Google Play Services)
============================================
I was able to put together a library to display interstitial ads from Admob. You'll need to create an interstitial ad unit in Admob and use it with the initialization process.

Make sure the following is added to your AndroidManifest.xml.
B4X:
AddApplicationText(<activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>)

Here is code from the included sample app that shows how to use the library. Because the interstitial is running as an activity, I'm not able to raise an event in B4A when it is dismissed. Instead, you can check the status in Activity_Resume as shown in the code below.

A good practice would be to initialize the ad in Activity_Create, and then show the ad later when desired. This will allow the ad to fully load while other processes are taking place allowing you to show the ad immediately.

This is a beta version and the first library I've shared, so please report any problems.

NOTE: There is a bug in older versions of the SDK that causes a crash on some devices when you try to load the interstitial. Make sure you download an updated SDK from Admob.

1.1 Update: Fixed a bug in the library code used to raise the AdFailedToLoad event. Separated out the LoadAd routine from the Initialize routine. This allows you to load a new ad after an ad has been dismissed without re-initializing the class.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim mwAdInterstitial As mwAdmobInterstitial
    Dim Button1 As Button
    Dim Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    mwAdInterstitial.Initialize("mwadi","INSERT_YOUR_AD_UNIT_ID")
    mwAdInterstitial.LoadAd
    Activity.LoadLayout("1")
    Label2.Text="Attempting to load ad...  Please wait."
End Sub

Sub Activity_Resume
    If mwAdInterstitial.Status=mwAdInterstitial.Status_Dismissed Then Label2.Text="Ad dismissed by user."
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub mwadi_AdLoaded
    Log("ad loaded")
    Label2.Text="Ad loaded - Click Show Ad to Display"

End Sub

Sub mwadi_AdFailedToLoad (ErrorCode As String)
    Label2.Text="Ad failed to load with error code: " & ErrorCode
    Log("failed to load ad: " & ErrorCode)
End Sub

Sub Button1_Click
    If mwAdInterstitial.Status=mwAdInterstitial.Status_AdReadyToShow Then mwAdInterstitial.Show
    If mwAdInterstitial.Status=mwAdInterstitial.Status_Dismissed Then
        Label2.Text="Attempting to load ad...  Please wait."
        mwAdInterstitial.LoadAd
    End If
End Sub
 

Attachments

  • interstitial.png
    interstitial.png
    86 KB · Views: 779
  • AdmobInterstitial1.1.zip
    10.6 KB · Views: 1,053
  • AdmobInterstitialPlayServices.zip
    11.5 KB · Views: 1,061
Last edited:

CyclopDroid

Well-Known Member
Licensed User
Longtime User
Hi. I want use, in my new game, a Interstintial with only video (25 second longer) and no immage. More game use this metod and I wantuse only for get a coin in my game.
What library use it? and hve exmple? Thanks ;)
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
I'm use interstitial rewared but if a click the back button, the video stopped.
How to test if the interstitial as view? How to set a time block (25 or other second, before appear the close button?
Most interstital video have this (but only 5 second)...but if I press the back button, it's closed :(
I want to permit back button only the video are no t charging (internet proble or other) and if no are see no close a button to lauch admob!
 

Douglas Farias

Expert
Licensed User
Longtime User
@Jack Cole
The admob did an update and added a new type of ad format.
Interstitial Awarded (have not tried, but obviously are video ads.)

You have plans to update this library for this new function?

thx for this lib.
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
I'm already using these. But the operation is the same? Or should I implement other code?
I wanted the Interstital that gave the skip after a certain number of seconds.
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
MmmhI've blocked to Interstintial Rewared :(
I use AdMob and view the video but...if I pree Back Key, return to game (no attend the second for charge video).
I'm not ure where this is then Rewardwhen I view but in the AdMob page is it. :(My question is...is there?

1It's possible when ot have a example code to B4A for use this type of Ads?What is the Ads prefer...this of AdMob or MobPub (only for Ads Rewared)? Are sure when AdMob not Ban (i've only here account)?
Thanks

upload_2016-4-22_16-58-33.png
 

CyclopDroid

Well-Known Member
Licensed User
Longtime User
mmmh Jack I need a help.:confused:
As you can listen to the events? I'll explain. When a Intertitial is loaded, if I press Back or I await the expiry of seconds before closing it by pressing the X button, I find myself always the Status = 5 (Dismised). There is no way to have a positive value when advertising is seen and when not?
How do I know if the user did not close the advertisement before? How to block the exit from this, without closing the X button is pressed?

Thanks for the help ... I'm going crazy. :D
 
Top