Android Question User Consent with Native Ads

Erel

Administrator
Staff member
Licensed User
I guess that you are asking about Builder.NonPersonalizedAds as all other things are the same.
B4X:
Dim AdRequestBuilder As AdRequestBuilder = Builder 'JavaObject builder
AdRequestBuilder.NonPersonalizedAds
'continue with Builder
 

Jack Cole

Active Member
Licensed 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.
 

Erel

Administrator
Staff member
Licensed User
The code I posted does what you need.
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.
Why would it be easier? It is exactly two simple lines of code...
 

Erel

Administrator
Staff member
Licensed 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)
 

asales

Well-Known Member
Licensed 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
 

Erel

Administrator
Staff member
Licensed User
Change to:
B4X:
Dim AdRequestBuilder1 As AdRequestBuilder = Builder 'JavaObject builder
AdRequestBuilder1.NonPersonalizedAds
'continue with Builder
 

Jack Cole

Active Member
Licensed User
If you do that, then you get an unknown member: runmethod error at:
B4X:
AdLoader.RunMethod("loadAd", Array(AdRequestBuilder.RunMethod("build", Null)))
 
Last edited:

Erel

Administrator
Staff member
Licensed 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
 

Jack Cole

Active Member
Licensed 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)))
 
Top