B4A Library HMS Ads

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

B4X founder
Staff member
Licensed User
Longtime 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
Longtime User
Download my example app and check subs "ShowConsentDialog" and "WebViewConsent_OverrideUrl" and "HuaweiAdsKitConsent1_UpdateConsentStatus"
 

skaliwag

Member
Licensed User
Longtime User

Are there methods available to Pause / Resume the Banner Ad, or are these not required?
 

Ferdari

Active Member
Licensed User
Longtime User
Hi @Erel thanks for the library, will be Rewarded ads available in a future version?
 

Ferdari

Active Member
Licensed User
Longtime User
Hi @Erel , im trying to load an interstitial ad

i declared iad on Globals
Dim iad As JavaObject

Then tried your code on Activity_Create:
B4X:
iad = hms.CreateInterstitialAd("testb4znbuh3n2", Me, "Interstitial") 'iad is a global JavaObject variable
hms.LoadInterstitialAd(iad, hms.CreateRequestBuilder)

But error thrown when loading IAD:
Error:
Registro conectado a:  LGE LM-V500N
--------- beginning of system
--------- beginning of main
** Activity (main) Create, isFirst = true **
hmsmanager_vv7 (java line: 357)
java.lang.RuntimeException: Constructor not found.
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:95)
    at ferdari.primeros_auxilios.english.hmsmanager._vv7(hmsmanager.java:357)
    at ferdari.primeros_auxilios.english.hmsmanager._vv6(hmsmanager.java:343)
    at ferdari.primeros_auxilios.english.main._activity_create(main.java:544)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at ferdari.primeros_auxilios.english.main.afterFirstLayout(main.java:105)
    at ferdari.primeros_auxilios.english.main.access$000(main.java:17)
    at ferdari.primeros_auxilios.english.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7573)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:937)
java.lang.RuntimeException: Constructor not found.

I have latest AppGallery and HMS Core installed, also set Manifest and added all dependencies as stated.

The Huawei Analytics Console is reporting my phone as connected real-time successfully

ObfuscatorMap:
vv6=createinterstitialad
vv7=createlistener

Do you know what could happen?
Your help is very appreciated.
 

Ferdari

Active Member
Licensed User
Longtime User
Do you have this line in the main module:
B4X:
#AdditionalJar: hms-ads-interstitial-13.4.30.301.aar
?
Its added, i fixed by calling in a class module
B4X:
iad = hms.CreateInterstitialAd("testb4znbuh3n2", Me, "Interstitial")
'to
Dim HMSModule as HMSClass
iad = hms.CreateInterstitialAd("testb4znbuh3n2", HMSModule, "Interstitial")
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…