Android Question Handling ads correctly

Spright

Active Member
I'm trying to figure out how to handle the ads correctly, I get this message in the log now, Is this message really supposed to be in the background or should you handle it somehow and avoid loading ad? I include the log output that i am reffering to at the top of the code! Otherwise, thanks for an amazing platform to develop software on!

It does need the libgdx library that is available on the forum. Otherwise it uses standard right out of the box.





B4X:
#MultiDex: True

#IF THEN
** Activity (main) Resume **
sensor listener setup
resumed
GOT REWARD! NOW YOU HAVE COLLECTED THIS MANY COINS:
ad closed
Failed to load RewardedAd: {
  "Code": 0,
  "Message": "Error while connecting to ad server: Unable to resolve host \"googleads.g.doubleclick.net\": No address associated with hostname",
  "Domain": "com.google.android.gms.ads",
  "Cause": "null",
  "Response Info": {
    "Response ID": "null",
    "Mediation Adapter Class Name": "",
    "Adapter Responses": [],
    "Response Extras": {}
  }
}
#End If

#Region Project Attributes
    #FullScreen: True
    #IncludeTitle: False
    #ApplicationLabel: Demo
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: sensorLandscape
    #CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#End Region

Sub Process_Globals
End Sub

Sub Globals
    Dim GL As lgGL
    Dim lGdx As LibGDX
    Dim IP As lgInputProcessor
    Dim Batch As lgSpriteBatch
    Dim texsheet As lgTexture
    Dim adwanted As Boolean = True
    Dim AdView1 As AdView
    Dim tm As Timer
    Dim Camera As lgOrthographicCamera
    Type MatrixValues(a As Float, b As Float, c As Float, d As Float, tx As Float, ty As Float)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim Config As lgConfiguration
    Config.useAccelerometer = False
    Config.useCompass = False
    lGdx.Initialize2(Config, "LG")
    AdView1.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", AdView1.SIZE_SMART_BANNER)
    Activity.AddView(AdView1, 0, 100%y - 50dip, 100%x, 50dip)
    AdView1.LoadAd
    tm.Initialize("timer",4000*2)
    tm.Enabled = True
End Sub

Sub Activity_Resume
    Starter.RewardAd.Callback = Me   
    If AdView1.IsInitialized Then AdView1.resume
    If lGdx.IsInitialized Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If AdView1.IsInitialized Then AdView1.pause
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

Sub Activity_Destroy
End Sub

Sub LG_Create
    Batch.Initialize
    IP.Initialize("IP")
    texsheet.Initialize("sprite.png")
    Camera.Initialize   
    Camera.SetToOrtho(False)
End Sub

Sub LG_Resume
End Sub

Sub LG_Pause
End Sub

Sub LG_Dispose
    If texsheet.IsInitialized Then texsheet.dispose
    If Batch.IsInitialized Then Batch.dispose
End Sub

Sub LG_Render
    GL.glClearColor(0, 0, 1, 1)
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
    Camera.Update
    Batch.ProjectionMatrix = Camera.Combined
    Batch.Begin
    Dim X, Y As Int
    For i = 1 To 99
        X = Rnd(0,999)
        Y = Rnd(0,499)
        Batch.DrawTex2(texsheet, X, Y, 100dip, 100dip)
    Next
    Batch.End
End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    Return True
End Sub

Sub ToastMsg_Show(Text As Object)
    ToastMessageShow(Text, True)
End Sub

Sub RewardAd_Rewarded (Item As Object)
    Log("GOT REWARD! NOW YOU HAVE COLLECTED THIS MANY COINS:")
End Sub

Sub RewardAd_AdClosed
    Log("ad closed")
End Sub

Sub timer_Tick
    If adwanted = True Then
        If Starter.RewardAd.Ready Then
            Starter.RewardAd.Show
            adwanted = False
            adwanted = True
        Else
            ' No ad is available, handle the error
            HandleAdLoadError
        End If
    End If
    Log("TIMER!")
End Sub

Sub HandleAdLoadError
    ToastMessageShow("Failed to load rewarded ad. Please check your internet connection.", True)
    ' Additional actions here e.g. disabling ad-related functionality or providing alternative content
End Sub
 

Attachments

  • demo.zip
    22.5 KB · Views: 131

asales

Expert
Licensed User
Longtime User
1 - Don't use AdView1.SIZE_SMART_BANNER
Use Adaptive Banner

2 - You can find the errors codes here:

3 - Check this example:
 
Upvote 0

Spright

Active Member
1 - Don't use AdView1.SIZE_SMART_BANNER
Use Adaptive Banner

2 - You can find the errors codes here:

3 - Check this example:
Thanks, i considered that one and others before i choose smart ads, for my use this time it's fine.
 
Upvote 0

Spright

Active Member
I'm not sure if i should keep bannerads if they blow up the complexty. I tried this but it's simply not woking?

B4X:
#MultiDex: True

#IF THEN
** Activity (main) Resume **
sensor listener setup
resumed
GOT REWARD! NOW YOU HAVE COLLECTED THIS MANY COINS:
ad closed
Failed to load RewardedAd: {
  "Code": 0,
  "Message": "Error while connecting to ad server: Unable to resolve host \"googleads.g.doubleclick.net\": No address associated with hostname",
  "Domain": "com.google.android.gms.ads",
  "Cause": "null",
  "Response Info": {
    "Response ID": "null",
    "Mediation Adapter Class Name": "",
    "Adapter Responses": [],
    "Response Extras": {}
  }
}
#End If

#Region Project Attributes
    #FullScreen: True
    #IncludeTitle: False
    #ApplicationLabel: Demo
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: sensorLandscape
    #CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#End Region

Sub GetAdaptiveAdSize As AdSize
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim width As Int = 100%x / GetDeviceLayoutValues.Scale
    Dim res As AdSize
    res.Native = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationAnchoredAdaptiveBannerAdSize", Array(ctxt, width))
    Dim jo As JavaObject = res.Native
    res.Width = jo.RunMethod("getWidthInPixels", Array(ctxt))
    res.Height = jo.RunMethod("getHeightInPixels", Array(ctxt))
    Return res
End Sub

Sub Process_Globals
End Sub

Sub Globals
    Type AdSize (Native As Object, Width As Int, Height As Int)
    Dim GL As lgGL
    Dim lGdx As LibGDX
    Dim IP As lgInputProcessor
    Dim Batch As lgSpriteBatch
    Dim texsheet As lgTexture
    Dim adwanted As Boolean = True
    Dim AdView1 As AdView
    Dim tm As Timer
    Dim Camera As lgOrthographicCamera
    Type MatrixValues(a As Float, b As Float, c As Float, d As Float, tx As Float, ty As Float)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim Config As lgConfiguration
    Config.useAccelerometer = False
    Config.useCompass = False
    lGdx.Initialize2(Config, "LG")
    Dim screenHeight As Int
    Dim adHeight As Int = 50dip ' Adjust the height of the ad banner as needed
    screenHeight = 100%y ' maybe need read correct size
 '   AdView1.Initialize2("BannerAd", "ca-app-pub-3940256099942544/6300978111", AdView1.SIZE_SMART_BANNER)
 '   Activity.AddView(AdView1, 0, screenHeight - adHeight, 100%x, adHeight)
 '   AdView1.LoadAd
 
    Dim size As AdSize = GetAdaptiveAdSize
    AdView1.Initialize2("ad", "ca-app-pub-126753333333/948333333", size.Native)
    Activity.AddView(AdView1, 0, 0,  size.Width, size.Height)   
                    ' (AdView1, 0, screenHeight - adHeight, 100%x, adHeight)
    AdView1.LoadAd
    
    tm.Initialize("timer", 4000 * 2)
    tm.Enabled = True
End Sub

Sub Activity_Resume
    Starter.RewardAd.Callback = Me   
    If AdView1.IsInitialized Then AdView1.resume
    If lGdx.IsInitialized Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If AdView1.IsInitialized Then AdView1.pause
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

Sub Activity_Destroy
End Sub

Sub LG_Create
    Batch.Initialize
    IP.Initialize("IP")
    texsheet.Initialize("sprite.png")
    Camera.Initialize   
    Camera.SetToOrtho(False)
End Sub

Sub LG_Resume
End Sub

Sub LG_Pause
End Sub

Sub LG_Dispose
    If texsheet.IsInitialized Then texsheet.dispose
    If Batch.IsInitialized Then Batch.dispose
End Sub

Sub LG_Render
    GL.glClearColor(0, 0, 1, 1)
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
    Camera.Update
    Batch.ProjectionMatrix = Camera.Combined
    Batch.Begin
    Dim X, Y As Int
    For i = 1 To 99
        X = Rnd(0,999)
        Y = Rnd(0,499)
        Batch.DrawTex2(texsheet, X, Y, 100dip, 100dip)
    Next
    Batch.End
End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    Return True
End Sub

Sub ToastMsg_Show(Text As Object)
    ToastMessageShow(Text, True)
End Sub

Sub RewardAd_Rewarded (Item As Object)
    Log("GOT REWARD! NOW YOU HAVE COLLECTED THIS MANY COINS:")
End Sub

Sub RewardAd_AdClosed
    Log("ad closed")
End Sub

Sub timer_Tick
    If adwanted = True Then
        If Starter.RewardAd.Ready Then
            Starter.RewardAd.Show
            adwanted = False
            adwanted = True
        Else
            ' No ad is available, handle the error
            HandleAdLoadError
        End If
    End If
    Log("TIMER!")
End Sub

Sub HandleAdLoadError
    ToastMessageShow("Failed to load rewarded ad. Please check your internet connection.", True)
    ' Additional actions here e.g. disabling ad-related functionality or providing alternative content
End Sub
 

Attachments

  • demo2.zip
    24.4 KB · Views: 117
Upvote 0

Spright

Active Member
I tried some other id and some other ocde and got it working but i noticed that not only me but many had problems with getting adaptive banners working in landscape. It does not show up on all my devices and one of the explanantion seemd to be that aaptive banners does not work correctly with widths larger than 2560 but I did not found a solution.

But there si a huge problem with adaptive banners and landscape mode. Smartbanners works but as you said they cannot be used.

so I changed the game into portrait to make life easier. I just could not solve it.
 
Upvote 0

Spright

Active Member
I solved it I think, i sitll have not tried it in landscape but i am pretty sure this is correct, you add the ad into a panel view.


B4X:
Build1=Default,b4a.example
File1=sprite.png
FileGroup1=Default Group
Group=Default Group
Library1=audio
Library10=phone
Library11=randomaccessfile
Library12=reflection
Library13=runtimepermissions
Library2=b4xpages
Library3=contentresolver
Library4=core
Library5=fileprovider
Library6=firebaseadmob2
Library7=javaobject
Library8=libgdx
Library9=libgdx_specialfx
ManifestCode=~\n~'This code will be applied to the manifest file during compilation.~\n~'You do not need to modify it in most cases.~\n~'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136~\n~AddManifestText(~\n~<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="31"/>~\n~<supports-screens android:largeScreens="true" ~\n~    android:normalScreens="true" ~\n~    android:smallScreens="true" ~\n~    android:anyDensity="true"/>    )~\n~~\n~SetApplicationAttribute(android:icon, "@drawable/icon")~\n~SetApplicationAttribute(android:label, "$LABEL$")~\n~ CreateResourceFromFile(Macro, Themes.DarkTheme)~\n~'End of default text.~\n~CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)~\n~CreateResourceFromFile (Macro, FirebaseAdMob.FirebaseAds)~\n~AddReplacement($ADMOB_APP_ID$, ca-app-pub-3940256099942544~3347511713)
Module1=arfRewardedAd
Module2=|relative|..\B4XMainPage
Module3=Starter
NumberOfFiles=1
NumberOfLibraries=13
NumberOfModules=3
Version=12.2
@EndOfDesignText@

' MODERN MINI BASE, uses panel
' Want to exploire multioortinetaion
' panel for ad (either portrait and landscape or both)
' position screen at mor esensible position knowing ads etc on scren
' maybe one less scale to do above even more sensible

#MultiDex: True

#Region Module Attributes
    #FullScreen: True
    #IncludeTitle: False
    #ApplicationLabel: Demo
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: Portrait
      #CanInstallToExternalStorage: False   
#End Region

Sub Process_Globals
End Sub

Sub Globals
    Type AdSize (Native As Object, Width As Int, Height As Int)
    Dim coins As Int
    Dim adwanted As Boolean = True
    Dim tm As Timer
    Dim lGdx As LibGDX
    Dim GL As lgGL
    Dim Surface As View
    Dim IP As lgInputProcessor
    Dim Batch As lgSpriteBatch
    Dim Texture As lgTexture
    Dim counter As Int
    Dim AdView1 As AdView
    Dim awidth As Int
    Dim aheight As Int
    Dim intwidth As Int
    Dim intheight As Int
    Dim startx As Int
    Dim starty As Int
    Dim vwidth As Int = 200
    Dim vheight As Int = 320
    Dim d As Boolean
    Dim g0 As lgMathMatrix4
    Dim g1 As lgMathMatrix4
    Dim tempmatrix As lgMathMatrix4
    Dim idmatrix As lgMathMatrix4
    Dim tempmatrix2 As lgMathMatrix4
End Sub

#IF THEN
Sub Activity_Create(FirstTime As Boolean)
    fullscreen
    If FirstTime = True Then
        Dim Config As lgConfiguration
        Config.useAccelerometer = False
        Config.useCompass = False
        Config.useWakelock = True
        Config.hideStatusBar = True
        Config.useImmersiveMode = True
        Config.r = 8
        Config.g = 8
        Config.b = 8
        Config.a = 8
        Config.numSamples = 0
        Config.maxSimultaneousSounds = 8
        Surface = lGdx.InitializeView2(Config, "LG")
        Activity.AddView(Surface, 0, 0, 100%x, 100%y)
        BlockScreenshots
        tm.Initialize("timer",4000*4)
        tm.Enabled = True
    End If
    Dim adHeight As Int = 50dip
    Dim screenHeight As Int = 100%y
    Dim size As AdSize = GetAdaptiveAdSize
    AdView1.Initialize2("ad", "ca-app-pub-3940256099942544/6300978111", size.Native)
    Dim adHeight As Int = size.height
    Activity.AddView(AdView1, 0, 0,  size.Width, adHeight)
    AdView1.LoadAd
End Sub
#END IF

Sub Activity_Create(FirstTime As Boolean)
        fullscreen
    If FirstTime = True Then
        Dim Config As lgConfiguration
        Config.useAccelerometer = False
        Config.useCompass = False
        Config.useWakelock = True
        Config.hideStatusBar = True
        Config.useImmersiveMode = True
        Config.r = 8
        Config.g = 8
        Config.b = 8
        Config.a = 8
        Config.numSamples = 0
        Config.maxSimultaneousSounds = 8
        Surface = lGdx.InitializeView2(Config, "LG")
        Activity.AddView(Surface, 0, 0, 100%x, 100%y)
        BlockScreenshots
        tm.Initialize("timer",4000*4)
        tm.Enabled = True
    End If
    
    ' Create the panel view and add it to the activity
    Dim panel As Panel
    panel.Initialize("")
    panel.Color = Colors.White
    panel.SetLayout(0, 0, 100%x, 90dip)
    Activity.AddView(panel, 0, 0, 100%x, 90dip)
    
    ' Initialize and load the adaptive ad inside the panel
    Dim size As AdSize = GetAdaptiveAdSize
    Dim adHeight As Int = size.Height
    Dim adView As AdView
    adView.Initialize2("ad", "ca-app-pub-3940256099942544/6300978111", size.Native)
    panel.Color = Colors.Transparent
    panel.AddView(adView, 0, 0, size.Width, adHeight)
    adView.LoadAd
End Sub


Sub GetAdaptiveAdSize As AdSize
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim width As Int = 100%x / GetDeviceLayoutValues.Scale
    ' width = Min(width,2560) ' Ad should be in panel
    Dim res As AdSize
    res.Native = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationAnchoredAdaptiveBannerAdSize", Array(ctxt, width))
    Dim jo As JavaObject = res.Native
    res.Width = jo.RunMethod("getWidthInPixels", Array(ctxt))
    res.Height = jo.RunMethod("getHeightInPixels", Array(ctxt))
    Return res
End Sub
 
Sub Activity_Resume
    Starter.RewardAd.Callback = Me
    If AdView1.IsInitialized Then AdView1.resume
    If lGdx.IsInitialized Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If AdView1.IsInitialized Then AdView1.pause
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

Sub Activity_Destroy
End Sub
 
Sub LG_Create   
    IP.Initialize("IP")
    Batch.Initialize
    Texture.Initialize("sprite.png")
    Texture.SetFilter(Texture.FILTER_Nearest, Texture.FILTER_Nearest)
    ResetMatrix
    tempmatrix2.setToOrtho2D(0,0,awidth,aheight)
    tempmatrix2.scale(1,-1,1)
    tempmatrix2.translate(0,-aheight,0)
    Batch.ProjectionMatrix = tempmatrix2
End Sub

Sub LG_Render
    GL.glClearColor(0, 0, 1, 1)
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
    Batch.Begin
    tempmatrix = g1.cpy
    Batch.TransformMatrix = tempmatrix
    Dim X, Y As Int
    For i = 1 To 99
        X = Rnd(0,200-8)
        Y = Rnd(0,320-8)
        Batch.DrawTex2(Texture, X, Y, 8,8)
    Next
    Batch.End
End Sub
 
Sub LG_Resume
End Sub

Sub LG_Pause
End Sub

Sub LG_Dispose
    If Texture.IsInitialized Then Texture.dispose
    If Batch.IsInitialized Then Batch.dispose
End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    Return True
End Sub

Sub ToastMsg_Show(Text As Object)
    ToastMessageShow(Text, True)
End Sub

Sub RewardAd_Rewarded (Item As Object)
    Log("GOT REWARD! NOW YOU HAVE COLLECTED THIS MANY COINS:")
End Sub

Sub RewardAd_AdClosed
    Log("ad closed")
End Sub

Sub timer_Tick
    If adwanted = True Then
        If Starter.RewardAd.Ready Then
            Starter.RewardAd.Show
            adwanted = False
            adwanted = True
        Else
            ' No ad is available, handle the error
            HandleAdLoadError
        End If
    End If
    Log("TIMER!")
End Sub

Sub HandleAdLoadError
    ToastMessageShow("Failed to load rewarded ad. Please check your internet connection.", True)
    ' Additional actions here e.g. disabling ad-related functionality or providing alternative content
End Sub

Sub fullscreen
    Dim p As Phone
    If p.SdkVersion >= 28 Then
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        ctxt.RunMethodJO("getWindow", Null).RunMethodJO("getAttributes", Null).SetField("layoutInDisplayCutoutMode", 1)
    End If
    GetRealSize
    Dim lv As LayoutValues = GetRealSize
    Dim jo As JavaObject = Activity
    jo.RunMethod("setBottom", Array(lv.Height))
    jo.RunMethod("setRight", Array(lv.Width))
    awidth =  lv.Width
    aheight = lv.Height
    Activity.Height = lv.Height
    Activity.Width = lv.Width
    SetSystemUIVisibilityFlags
End Sub

Sub SetSystemUIVisibilityFlags
    Try
        Dim jo As JavaObject
        jo.InitializeContext
        jo = jo.RunMethod("getWindow", Null)
        Dim decorView As JavaObject = jo.RunMethod("getDecorView", Null)
        Dim decorView As JavaObject = jo.RunMethod("getDecorView", Null)
        Dim SYSTEM_UI_FLAG_LAYOUT_STABLE As Int = 256
        Dim SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION As Int = 512
        Dim SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN As Int = 1024
        Dim SYSTEM_UI_FLAG_HIDE_NAVIGATION As Int = 2
        Dim SYSTEM_UI_FLAG_FULLSCREEN As Int = 4
        Dim SYSTEM_UI_FLAG_IMMERSIVE As Int = 2048
        Dim flags As Int = SYSTEM_UI_FLAG_LAYOUT_STABLE + SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + SYSTEM_UI_FLAG_HIDE_NAVIGATION + SYSTEM_UI_FLAG_FULLSCREEN + SYSTEM_UI_FLAG_IMMERSIVE
        Log("FLAGS ARE")
        Log(flags)
        decorView.RunMethod("setSystemUiVisibility", Array As Object(flags))
    Catch
        Log("Error setting system UI visibility flags")
    End Try
End Sub

Sub GetRealSize As LayoutValues
    Dim lv As LayoutValues
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim WindowManager As JavaObject = ctxt.RunMethodJO("getSystemService", Array("window"))
    Dim display As JavaObject = WindowManager.RunMethod("getDefaultDisplay", Null)
    Dim point As JavaObject
    point.InitializeNewInstance("android.graphics.Point", Null)
    display.RunMethod("getRealSize", Array(point))
    lv.Width = point.GetField("x")
    lv.Height = point.GetField("y")
    lv.Scale = 100dip / 100
    Return lv
End Sub

Sub ResetMatrix
    d = False
    Dim sx As Int = 0, sy As Int = 0
    Dim awidthfloat As Float = awidth        ' Native resolution
    Dim aheightfloat As Float  = aheight
    Dim vwidthfloat As Float = vwidth        ' Virtual resolution
    Dim vheightfloat As Float = vheight
    intwidth = Floor(awidthfloat / vwidthfloat)
    intheight = Floor(aheightfloat / vheightfloat)
    If intwidth > intheight Then intwidth = intheight
    If intheight > intwidth Then intheight = intwidth
    If intwidth < 1 Then intwidth = 1
    If intheight < 1 Then intheight = 1
    startx = (awidth - (vwidth * intwidth)) / 2
    starty = (aheight - (vheight * intheight)) / 2
    g0 = idmatrix.cpy : g1  = idmatrix.cpy ' new matrix
    g1.translate(startx+(sx*intwidth),starty+(sy*intheight),0) ' order rev
    g1.scale(intwidth,intwidth,1.0) ' order rev
End Sub

' Screenshot protection, run after created the LG surface
Sub BlockScreenshots
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("securescreen", Null)
End Sub

Sub AllowScreenshots
    Dim jo As JavaObject
    jo.InitializeContext
    jo.RunMethod("unsecurescreen", Null)
End Sub

#If Java
    import android.annotation.TargetApi;
    import android.content.Context;
    import android.view.WindowManager.*;
    public void securescreen() {
        this.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
    }
    public void unsecurescreen() {
        this.getWindow().clearFlags(LayoutParams.FLAG_SECURE);
    }
#End If
 

Attachments

  • miniads.zip
    24.1 KB · Views: 123
Upvote 0

Spright

Active Member
Here's the final version I want to share, it uses adapative ads and got it peroperly working in any resolution,and orientation!

B4X:
 ' Slim
#MultiDex: True

#Region Project Attributes
    #FullScreen: True
    #IncludeTitle: False
    #ApplicationLabel: Demo
    #VersionCode: 1
    #VersionName:
    #SupportedOrientations: sensorLandscape
    #CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#End Region

Sub Process_Globals
End Sub

Sub Globals
    Type AdSize (Native As Object, Width As Int, Height As Int)
    Dim GL As lgGL
    Dim lGdx As LibGDX
    Dim IP As lgInputProcessor
    Dim Batch As lgSpriteBatch
    Dim texsheet As lgTexture
    Dim adwanted As Boolean = True
    Dim Surface As View
    Dim AdView1 As AdView
    Dim tm As Timer
    Dim Camera As lgOrthographicCamera
    Type MatrixValues(a As Float, b As Float, c As Float, d As Float, tx As Float, ty As Float)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        Dim Config As lgConfiguration
        Config.useAccelerometer = False
        Config.useCompass = False
        Config.useWakelock = True
        Config.hideStatusBar = True
        Config.useImmersiveMode = True
        Config.r = 8
        Config.g = 8
        Config.b = 8
        Config.a = 8
        Config.numSamples = 0
        Config.maxSimultaneousSounds = 8
        Surface = lGdx.InitializeView2(Config, "LG")
        Activity.AddView(Surface, 0, 0, 100%x, 100%y)
        tm.Initialize("timer",4000*4)
        tm.Enabled = True
    End If
    
    ' Create the panel view and add it to the activity
    Dim panel As Panel
    panel.Initialize("")
    panel.Color = Colors.White
    panel.SetLayout(0, 0, 100%x, 90dip)
    Activity.AddView(panel, 0, 0, 100%x, 90dip)
    
    ' Initialize and load the adaptive ad inside the panel
    Dim size As AdSize = GetAdaptiveAdSize
    Dim adHeight As Int = size.Height
    Dim adView As AdView
    adView.Initialize2("ad", "ca-app-pub-3940256099942544/6300978111", size.Native)
    panel.Color = Colors.Transparent
    panel.AddView(adView, 0, 0, size.Width, adHeight)
    adView.LoadAd
End Sub

Sub GetAdaptiveAdSize As AdSize
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim AdSize As JavaObject
    Dim width As Int = 100%x / GetDeviceLayoutValues.Scale
    ' width = Min(width,2560) ' Ad should be in panel
    Dim res As AdSize
    res.Native = AdSize.InitializeStatic("com.google.android.gms.ads.AdSize").RunMethod("getCurrentOrientationAnchoredAdaptiveBannerAdSize", Array(ctxt, width))
    Dim jo As JavaObject = res.Native
    res.Width = jo.RunMethod("getWidthInPixels", Array(ctxt))
    res.Height = jo.RunMethod("getHeightInPixels", Array(ctxt))
    Return res
End Sub
 
Sub Activity_Resume
    Starter.RewardAd.Callback = Me
    If AdView1.IsInitialized Then AdView1.resume
    If lGdx.IsInitialized Then lGdx.Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If AdView1.IsInitialized Then AdView1.pause
    If lGdx.IsInitialized Then lGdx.Pause
End Sub

Sub Activity_Destroy
End Sub
 
Sub LG_Create   
    Batch.Initialize
    IP.Initialize("IP")
    texsheet.Initialize("sprite.png")
    Camera.Initialize   
    Camera.SetToOrtho(False)
End Sub

Sub LG_Render   
    GL.glClearColor(0, 0, 1, 1)
    GL.glClear(GL.GL10_COLOR_BUFFER_BIT)
    Camera.Update
    Batch.ProjectionMatrix = Camera.Combined
    Batch.Begin
    Dim X, Y As Int
    For i = 1 To 99
        X = Rnd(0,999)
        Y = Rnd(0,499)
        Batch.DrawTex2(texsheet, X, Y, 100dip, 100dip)
    Next
    Batch.End
End Sub
 
Sub LG_Resume
End Sub

Sub LG_Pause
End Sub

Sub LG_Dispose
    If texsheet.IsInitialized Then texsheet.dispose
    If Batch.IsInitialized Then Batch.dispose
End Sub

Sub IP_TouchDown(ScreenX As Int, ScreenY As Int, Pointer As Int) As Boolean
    Return True
End Sub

Sub ToastMsg_Show(Text As Object)
    ToastMessageShow(Text, True)
End Sub

Sub RewardAd_Rewarded (Item As Object)
    Log("GOT REWARD! NOW YOU HAVE COLLECTED THIS MANY COINS:")
End Sub

Sub RewardAd_AdClosed
    Log("ad closed")
End Sub

Sub timer_Tick
    If adwanted = True Then
        If Starter.RewardAd.Ready Then
            Starter.RewardAd.Show
            adwanted = False
            adwanted = True
        Else
            ' No ad is available, handle the error
            HandleAdLoadError
        End If
    End If
    Log("TIMER!")
End Sub

Sub HandleAdLoadError
    ToastMessageShow("Failed to load rewarded ad. Please check your internet connection.", True)
    ' Additional actions here e.g. disabling ad-related functionality or providing alternative content
End Sub
 

Attachments

  • newdemo.zip
    22.7 KB · Views: 128
Upvote 0
Top