B4A Library display.io ads service

Similar threads

B4A Code Snippet Draw on top of other apps permission
B4A Tutorial Version safe APK installation
B4A Library Tappx ads service
B4A Library AppBrain Ads
B4A Code Snippet Access Wifi Information

Erel

Administrator
Staff member
Licensed User


1. Register with http://www.display.io/en/, create a new app and an interstitial ad unit.
2. Download their sdk and copy it to the additional libraries folder.
3. Add to manifest editor:
B4X:
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddApplicationText(
<activity android:name="io.display.sdk.DioActivity"
   android:configChanges="orientation|screenSize|keyboardHidden"/>
<activity android:name="io.display.sdk.DioTranslucentActivity"
   android:configChanges="orientation|screenSize|keyboardHidden"
   android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity android:name="io.display.sdk.device.PermissionsHandler" />
)
4. Dependencies:
B4X:
#AdditionalJar: com.android.support:appcompat-v7
#AdditionalJar: com.android.support:support-v4
#AdditionalJar: com.android.support:percent
#AdditionalJar: io.display.sdk-1.5.0
Note that the sdk version might be different. It is part of the jar file name.

5. Code:
B4X:
Sub Process_Globals
 
End Sub

Sub Globals
   Private DisplayIOController As JavaObject
   Private ctxt As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
   InitDisplayIO("1215")
End Sub

Sub InitDisplayIO (AppId As String)
   ctxt.InitializeContext
   DisplayIOController = DisplayIOController.InitializeStatic("io.display.sdk.Controller").RunMethodJO("getInstance", Null)
   DisplayIOController.RunMethod("init", Array(ctxt, AppId))
End Sub

Sub ShowInterstitial (UnitId As String)
   Dim res As Boolean = DisplayIOController.RunMethod("showAd", Array(ctxt, UnitId))
   Log(res)
End Sub

Sub Activity_Click
   ShowInterstitial("1848")
End Sub
Make sure to update the AppId and UnitId.

Depends on JavaObject.

Update:

- A developer who used display.io in the past has contacted me and said that based on his experience he suspects that display.io ads do not comply with Google policies and can cause your app to be suspended.
- It is important to note that Anywhere Software is not affiliated with any ad service and we don't endorse any third party service.
- I'm just providing the simple code that allows to directly access their SDK.
- I hope that a representative from display.io will be able to clarify it.
 
Last edited:

sorex

Expert
Licensed User
Thanks.

Is this the direct wrapless method to use their sdk?

There were B4A libraries before but all of them had their issues and made by different people for them.

Reporting the issues lead to a dead end of promises so using the sdk that works right is always a plus.
 

Douglas Farias

Expert
Licensed User
Hi.

Please, can you or someone with knowledge in libs update this example? Currently it is no longer working, I believe the io display has had some updates.

I noticed that there were some changes in SKD and even some added listeners.

SDK

ARR download (4.4.1 the last)

Java sample app

I tried to download the latest .arr from the sdk and unfortunately the example does not compile, it displays the following error.
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:20: error: No resource identifier found for attribute 'layout_constraintBottom_toBottomOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:20: error: No resource identifier found for attribute 'layout_constraintStart_toStartOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:30: error: No resource identifier found for attribute 'layout_constraintBottom_toBottomOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:30: error: No resource identifier found for attribute 'layout_constraintEnd_toEndOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:48: error: No resource identifier found for attribute 'layout_constraintEnd_toEndOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:48: error: No resource identifier found for attribute 'layout_constraintTop_toTopOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:61: error: No resource identifier found for attribute 'layout_constraintBottom_toBottomOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:61: error: No resource identifier found for attribute 'layout_constraintEnd_toEndOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:81: error: No resource identifier found for attribute 'layout_constraintBottom_toBottomOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:81: error: No resource identifier found for attribute 'layout_constraintEnd_toEndOf' in package 'teste.app.iddouglasdisplay'
C:\android\tools\..\extras\b4a_local\unpacked-io.display.sdk-4.4.1-63729591276479\res\layout\ad_item_layout.xml:81: error: No resource identifier found for attribute 'layout_constraintStart_toStartOf' in package 'teste.app.iddouglasdisplay'
Thank you!
I'm here if you need help with testing.
 
Last edited:

Douglas Farias

Expert
Licensed User
Hello everyone.
Complementing the Erel example, below is an example using the latest SDK version (4.4.5) and also using banner.

Download the 4.4.5 arr file.
B4X:
#AdditionalJar: sdk-4.4.5.aar
#AdditionalJar: com.android.support.constraint:constraint-layout
Manifest
XML:
'DISPLAY IO
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_NETWORK_STATE)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddApplicationText(
<activity android:name="com.brandio.ads.DioActivity"
   android:configChanges="orientation|screenSize|keyboardHidden"/>
<activity android:name="com.brandio.ads.DioTranslucentActivity"
   android:configChanges="orientation|screenSize|keyboardHidden"
   android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<activity android:name="com.brandio.ads.device.PermissionsHandler" />
)
Service
B4X:
#Region  Service Attributes
    #StartAtBoot: False
#End Region

Sub Process_Globals
  
    'GENERAL
    Private appId As String = "xxxx"
    Private controllerDisplayio As JavaObject
    Private ctxt As JavaObject
    Private SdkListener As Object
    Private retryTime As Int = 30000
    Private reloadTime As Int = 2000

    'INTER
    Private intersticialAd As JavaObject
    Private intersticialPlacementId As String = "xxxx"
    Private intersticialPlacement As JavaObject
    Public isInterstitialReady As Boolean = False
  
    'BANNER
    Public bannerView As JavaObject
    Private bannerPlacementId As String = "xxxx"
    Private bannerPlacement As JavaObject
    Private AdRequestBanner As JavaObject
    Public isBannerReady As Boolean = False

End Sub

Sub Service_Create
  
    'INITIALIZE CONTEXT
    ctxt.InitializeContext

    'INITIALIZE SDK
    controllerDisplayio = controllerDisplayio.InitializeStatic("com.brandio.ads.Controller").RunMethod("getInstance", Null)
    SdkListener = controllerDisplayio.CreateEventFromUI("com.brandio.ads.listeners.SdkInitListener", "SDK", Null)
    controllerDisplayio.RunMethod("init", Array(ctxt, Null, appId, SdkListener))
  
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground
End Sub

Sub Service_Destroy

End Sub


Sub SDK_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
      
        Case "onInit"
            LogColor("onInit SDK Displayio", Colors.Yellow)
            intersticialPlacement = controllerDisplayio.RunMethod("getPlacement", Array(intersticialPlacementId))
            bannerPlacement = controllerDisplayio.RunMethod("getPlacement", Array(bannerPlacementId))
            Get_Inter
            Get_Banner
          
        Case "onInitError"
            LogColor("onInitError SDK Displayio", Colors.Yellow)
      
    End Select
    Return Null
End Sub





#Region INTER

Sub Get_Inter
  
    Private AdRequest As JavaObject = intersticialPlacement.RunMethod("newAdRequest", Null)
    Dim AdRequestListener As Object = AdRequest.CreateEventFromUI("com.brandio.ads.listeners.AdRequestListener", "adRequestIntersticial", Null)
    AdRequest.RunMethod("setAdRequestListener", Array(AdRequestListener))
    AdRequest.RunMethod("requestAd", Null)
  
End Sub


Sub adRequestIntersticial_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
      
        Case "onAdReceived"
            LogColor("onAdReceived Inter Displayio", Colors.Yellow)
            Private AdProvider As JavaObject = Args(0)
            Dim AdLoadListener As Object = AdProvider.CreateEventFromUI("com.brandio.ads.listeners.AdLoadListener", "adLoadIntersticial", Null)
            AdProvider.RunMethod("setAdLoadListener", Array(AdLoadListener))
          
            Try
                AdProvider.RunMethod("loadAd", Null)
            Catch
                If LastException.IsInitialized Then Log(LastException)
                Retry_Inter
            End Try
          
          
        Case "onNoAds"
            LogColor("onNoAds Inter Displayio", Colors.Yellow)
            isInterstitialReady = False
            Retry_Inter
      
    End Select
    Return Null
End Sub


Sub adLoadIntersticial_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
      
        Case "onLoaded"
            LogColor("onLoaded Inter Displayio", Colors.Yellow)
            intersticialAd = Args(0)
            Dim AdEventListener As Object = intersticialAd.CreateEventFromUI("com.brandio.ads.listeners.AdEventListener", "AdEventIntersticial", Null)
            intersticialAd.RunMethod("setEventListener", Array(AdEventListener))
            isInterstitialReady = True
      
      
        Case "onFailedToLoad"
            LogColor("onFailedToLoad Inter Displayio", Colors.Yellow)
            isInterstitialReady = False
            Retry_Inter
          
    End Select
    Return Null
End Sub


Sub AdEventIntersticial_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
      
    Case "onShown"
        LogColor("onShown Inter Displayio", Colors.Yellow)
        isInterstitialReady = False
  
    Case "onFailedToShow"
        LogColor("onFailedToShow Inter Displayio", Colors.Yellow)
        isInterstitialReady = False
        Reload_Inter
  
    Case "onClicked"
        LogColor("onClicked Inter Displayio", Colors.Yellow)
        isInterstitialReady = False
  
    Case "onClosed"
        LogColor("onClosed Inter Displayio", Colors.Yellow)
        isInterstitialReady = False
        Reload_Inter

    Case "onAdCompleted"
        isInterstitialReady = False
        LogColor("onAdCompleted Inter Displayio", Colors.Yellow)
  
    End Select
    Return Null
End Sub

Sub Retry_Inter
    isInterstitialReady = False
    Sleep(retryTime)
    Get_Inter
End Sub

Sub Reload_Inter
    isInterstitialReady = False
    Sleep(reloadTime)
    Get_Inter
End Sub

Sub Show_Inter
    intersticialAd.RunMethod("showAd", Array(ctxt))
End Sub

#End Region






#Region BANNER

Sub Get_Banner

    AdRequestBanner = bannerPlacement.RunMethod("newAdRequest", Null)
    Dim AdRequestListener As Object = AdRequestBanner.CreateEventFromUI("com.brandio.ads.listeners.AdRequestListener", "adRequestBanner", Null)
    AdRequestBanner.RunMethod("setAdRequestListener", Array(AdRequestListener))
    AdRequestBanner.RunMethod("requestAd", Null)
  
End Sub


Sub adRequestBanner_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
      
        Case "onAdReceived"
            LogColor("onAdReceived Banner Displayio", Colors.Yellow)
            Private AdProvider As JavaObject = Args(0)
            Dim AdLoadListener As Object = AdProvider.CreateEventFromUI("com.brandio.ads.listeners.AdLoadListener", "adLoadBanner", Null)
            AdProvider.RunMethod("setAdLoadListener", Array(AdLoadListener))
          
            Try
                AdProvider.RunMethod("loadAd", Null)
            Catch
                If LastException.IsInitialized Then Log(LastException)
                Retry_Banner
            End Try
          
      
        Case "onNoAds"
            LogColor("onNoAds Banner Displayio", Colors.Yellow)
            isBannerReady = False
            Retry_Banner
      
    End Select
    Return Null     
End Sub


Sub adLoadBanner_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
  
        Case "onLoaded"
            LogColor("onLoaded Banner Displayio", Colors.Yellow)
            bannerView = bannerPlacement.RunMethod("getBanner", Array(ctxt, AdRequestBanner.RunMethod("getId", Null)))
            isBannerReady = True
      
      
        Case "onFailedToLoad"
            LogColor("onFailedToLoad Banner Displayio", Colors.Yellow)
            isBannerReady = False
            Retry_Banner

    End Select
    Return Null
End Sub


Sub Retry_Banner
    isBannerReady = False
    Sleep(retryTime)
    Get_Banner
End Sub

#End Region
I use it in a service (where all events work correctly), but you can compile it as a library or use it as a class.

Interstitial
B4X:
If Not(IsPaused(displayio)) And displayio.isInterstitialReady Then CallSub(displayio, "Show_Inter")
Banner
B4X:
If Not(IsPaused(displayio)) And displayio.isBannerReady Then
    Private bannerView As View
    bannerView = displayio.bannerView
    Activity.AddView(bannerView,0%x, 100%y - 51dip, 100%x, 51dip)
End if

Thx
 
Top