1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question User Consent with Native Ads

Discussion in 'Android Questions' started by Jack Cole, Jun 26, 2019.

  1. Jack Cole

    Jack Cole Active Member Licensed User

  2. Erel

    Erel Administrator Staff Member Licensed User

    I guess that you are asking about Builder.NonPersonalizedAds as all other things are the same.
    Code:
    Dim AdRequestBuilder As AdRequestBuilder = Builder 'JavaObject builder
    AdRequestBuilder.NonPersonalizedAds
    'continue with Builder
     
  3. Jack Cole

    Jack Cole Active Member Licensed User

    Yes, that is correct. There are other parts I'm uncertain about. Consider the following code:

    Code:
    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.

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

    Erel Administrator Staff Member Licensed User

    The code I posted does what you need.
    Why would it be easier? It is exactly two simple lines of code...
     
  5. Jack Cole

    Jack Cole Active Member Licensed User

    If it does, I can't see how. How does it handle the ContentAdLoaded event?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    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)
     
  7. asales

    asales Well-Known Member Licensed User

    I tested the new code in the original code, but I got errors:
    Code:
    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:
    Code:
    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:
    Code:
    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
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Change to:
    Code:
    Dim AdRequestBuilder1 As AdRequestBuilder = Builder 'JavaObject builder
    AdRequestBuilder1.NonPersonalizedAds
    'continue with Builder
     
  9. Jack Cole

    Jack Cole Active Member Licensed User

    If you do that, then you get an unknown member: runmethod error at:
    Code:
    AdLoader.RunMethod("loadAd"Array(AdRequestBuilder.RunMethod("build"Null)))
     
    Last edited: Jun 30, 2019
  10. Erel

    Erel Administrator Staff Member Licensed User

    My mistake. It should be added like this:
    Code:
    AdRequestBuilder.InitializeNewInstance("com/google/android/gms/ads/AdRequest.Builder".Replace("/""."), Null)
       
    Dim AdRequestBuilder1 As AdRequestBuilder = AdRequestBuilder 'JavaObject builder '<---- new 
       AdRequestBuilder1.NonPersonalizedAds
       
    'continue with AdRequestBuilder
     
  11. Jack Cole

    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.

    Code:
    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.

    Code:
    AdLoader.RunMethod("loadAd"Array(AdrequestBuilder.RunMethod("build"Null)))
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    They both point to the same object.
     
    Jack Cole likes this.
  13. Jack Cole

    Jack Cole Active Member Licensed User

    Perfect. Thanks again for your help.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice