B4A Library HMS Ads

Erel

Administrator
Staff member
Licensed User
HMS v1.02 adds support for banner and interstitial ads.
Make sure to update the dependencies list and the manifest editor items.

The code is simple:

B4X:
'Banner
Dim banner As B4XView = hms.CreateBannerAd("testw6vs28auh3", "BANNER_SIZE_320_50", Me, "Banner")
Root.AddView(banner, Root.Width / 2 - 320dip / 2, Root.Height - 50dip, 320dip, 50dip)
hms.LoadBannerAd(banner, hms.CreateRequestBuilder)

Sub Banner_AdFailed (ErrorCode As Int)
    Log("Failed: " & ErrorCode)
End Sub

Sub Banner_AdLoaded
    Log("AdLoaded")
End Sub

Sub Banner_AdOpened
    Log("AdOpened")
End Sub

'Interstitial
iad = hms.CreateInterstitialAd("testb4znbuh3n2", Me, "Interstitial") 'iad is a global JavaObject variable
hms.LoadInterstitialAd(iad, hms.CreateRequestBuilder)

Sub Interstitial_AdFailed (ErrorCode As Int)
    Log("Failed: " & ErrorCode)
End Sub

Sub Interstitial_AdLoaded
    Log("AdLoaded")
    iad.RunMethod("show", Null)
End Sub
The above ad ids are test ids.
The process of creating a real ad id is explained here: https://developer.huawei.com/consumer/en/doc/distribution/monetize/59500560
Note that I've tested it with the test ids only. If you encounter any issue with a real id then post here and we will help you solve it.

There is another HMS ads library, created by @Pendrush here: https://www.b4x.com/android/forum/threads/huawei-ads-kit.123030/#content
Note that the request builder options are set based on @Pendrush recommendations.
 

Erel

Administrator
Staff member
Licensed User
The request builder is set by default with:
B4X:
Public Sub CreateRequestBuilder As JavaObject
    Dim builder As JavaObject
    builder.InitializeNewInstance("com.huawei.hms.ads.AdParam$Builder", Null)
    builder.RunMethod("setAdContentClassification", Array("A"))
    Dim unspecified As Int = -1
    builder.RunMethod("setTagForUnderAgeOfPromise", Array(unspecified))
    builder.RunMethod("setTagForChildProtection", Array(unspecified))
    Return builder
End Sub
Are you talking about something else?
 

Pendrush

Well-Known Member
Licensed User
Download my example app and check subs "ShowConsentDialog" and "WebViewConsent_OverrideUrl" and "HuaweiAdsKitConsent1_UpdateConsentStatus"
 
Top