Android Question User Consent with Native Ads

Jack Cole

Well-Known Member
Licensed User
Longtime User
Yes, that is correct. There are other parts I'm uncertain about. Consider the following code:

B4X:
    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
    Dim OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/", "."), _
           "ContentAdLoaded", Null)
    builder.RunMethod("forContentAd", Array(OnContentAdLoadedListener))
  
    Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
    Dim AdRequestBuilder As JavaObject
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
    AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
    Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)

I'm thinking it would probably be easier to use the javaobject rather than using the AdRequestBuilder type that you referenced in your post above.

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("your device id from logcat") // Todo: Remove in Release build
.addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())
.build();
mAdView.loadAd(adRequest);

I don't understand the java side of things well enough to know how to implement ".addNetworkExtrasBundle(AdMobAdapter.class, getNonPersonalizedAdsBundle())" with JavaObject.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
    Dim OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/", "."), _
           "ContentAdLoaded", Null)
    builder.RunMethod("forContentAd", Array(OnContentAdLoadedListener))
 
Dim AdRequestBuilder As AdRequestBuilder = Builder 'JavaObject builder
AdRequestBuilder.NonPersonalizedAds
'continue with Builder

    Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
    Dim AdRequestBuilder As JavaObject
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
    AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
    Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)
 
Upvote 0

asales

Expert
Licensed User
Longtime User
I tested the new code in the original code, but I got errors:
B4X:
Unknow member: runmethod
Unknow member: initializenewinstance
Current declaration does not match previous one.
Previous: {Type=AdRequestBuilder,Rank=0,RemoteObject=True}
Current: {Type=JavaObject,Rank=0,RemoteObject=True}
on this lines:
B4X:
    Dim AdRequestBuilder As JavaObject
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
    AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))

"Dim AdRequestBuilder" was declared 2 times:
B4X:
Dim AdRequestBuilder As AdRequestBuilder = Builder 'JavaObject builder
Dim AdRequestBuilder As JavaObject

A tip: uses UnifiedNativeAd instead ContentAd or InstallAd:
https://developers.google.com/admob/android/native/start
https://www.b4x.com/android/forum/threads/native-ads-unified-with-mediaview.100350/#content
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
My mistake. It should be added like this:
B4X:
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
   Dim AdRequestBuilder1 As AdRequestBuilder = AdRequestBuilder 'JavaObject builder '<---- new 
   AdRequestBuilder1.NonPersonalizedAds
   'continue with AdRequestBuilder
 
Upvote 0

Jack Cole

Well-Known Member
Licensed User
Longtime User
Thank you Erel. Sorry to take up so much of your time on this. Here is my completed code.

B4X:
    Dim AdUnitId As String = AdID
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim builder As JavaObject
    builder.InitializeNewInstance("com.google.android.gms.ads.AdLoader.Builder", Array(ctxt, AdUnitId))
    Dim OnContentAdLoadedListener As Object = builder.CreateEventFromUI("com/google/android/gms/ads/formats/NativeContentAd.OnContentAdLoadedListener".Replace("/", "."), _
           "ContentAdLoaded", Null)
    builder.RunMethod("forContentAd", Array(OnContentAdLoadedListener))
  
    Dim AdLoader As JavaObject = builder.RunMethod("build", Null)
    Dim AdrequestBuilder As JavaObject
    AdrequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/", "."), Null)
    Dim AdRequestBuilder1 As AdRequestBuilder = AdrequestBuilder 'JavaObject builder
    If Starter.consent.IsRequestLocationInEeaOrUnknown Then   
        LogColor("EU or unknown",Colors.Magenta)
        AdRequestBuilder1.NonPersonalizedAds
    Else
        LogColor("non-EU",Colors.Magenta)
    End If
    LogColor("trying to load content ad",Colors.Red)
 
    AdLoader.RunMethod("loadAd", Array(AdrequestBuilder.RunMethod("build", Null)))
    
    Wait For ContentAdLoaded_Event (MethodName As String, Args() As Object)

I'm a bit concerned about the following line, because it seems like it could be using a different variable. Perhaps AdRequestBuilder1 and AdRequestBuilder become merged? If so, then I think everything is working fine.

B4X:
AdLoader.RunMethod("loadAd", Array(AdrequestBuilder.RunMethod("build", Null)))
 
Upvote 0
Top