Java Question How get activity context on a class module (compile to library)

Douglas Farias

Expert
Licensed User
Longtime User
Hi all.
i m working to make a startapp lib on B4A and now i have a problem..

the code below is working perfectly in an activity, however when I try to use it in a class it does not start correctly.
I intend to compile it as a library, to use in other projects.
B4X:
Sub Class_Globals
    Private StartAppSDK As JavaObject
    Private StartAppAd As JavaObject
    Private ctxt As JavaObject
    Public isReady As Boolean = False
    Private AdListenerInterstitial As Object
    Private AdDisplayListener As Object
    Private EventGeneral As String
End Sub


Public Sub Initialize(EventName As String, AppKey As String, testMode As Boolean)
  
    EventGeneral = EventName
    If Not(ctxt.IsInitialized) Then ctxt.InitializeContext
    If StartAppSDK.IsInitialized Then Return
  
    'INITIALIZE SDK
    StartAppSDK.InitializeStatic("com.startapp.sdk.adsbase.StartAppSDK")
    StartAppSDK.RunMethod("init", Array(ctxt,AppKey,True))
    StartAppSDK.RunMethod("setTestAdsEnabled", Array(testMode))

    'AD LISTENER
    StartAppAd.InitializeNewInstance("com.startapp.sdk.adsbase.StartAppAd", Array(ctxt))
    AdListenerInterstitial = StartAppAd.CreateEventFromUI("com.startapp.sdk.adsbase.adlisteners.AdEventListener", "AdListenerInterstitial", Null)
    AdDisplayListener = StartAppAd.CreateEventFromUI("com.startapp.sdk.adsbase.adlisteners.AdDisplayListener", "AdDisplayListener", Null)

End Sub


Private Sub AdListenerIntersticial_Event (MethodName As String, Args() As Object) As Object 'EVENTOS DO BANNER
    Select MethodName
      
        Case "onReceiveAd"
            If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral,"onReceiveAd")
            isReady = True
          
        Case "onFailedToReceiveAd"
            If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral,"FailedToReceiveAd")
            isReady = False
              
    End Select
    Return Null
End Sub


Private Sub AdDisplayListener_Event (MethodName As String, Args() As Object) As Object
    Select MethodName
      
        Case "adHidden"
            If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral,"adHidden")

        Case "adDisplayed"
            If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral,"adDisplayed")

        Case "adClicked"
            If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral,"adClicked")
          
        Case "adNotDisplayed"
            If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral,"adNotDisplayed")
          
    End Select
    Return Null
End Sub


Public Sub LoadAd
    If StartAppAd.IsInitialized Then StartAppAd.RunMethod("loadAd", Array(AdListenerInterstitial))
End Sub


Public Sub showAd
    If StartAppAd.IsInitialized And isReady Then StartAppAd.RunMethod("showAd", Array(AdDisplayListener))
End Sub

Instead of using
B4X:
ctxt.InitializeContext

How can I get the activity in which the class was started?

For example in main I started with the following command.
B4X:
interstitial.Initialize ("test", "999999", True)
interstitial.LoadAd

However, ctxt.InitializeContext is initialized with class and not as activity in which it was started.

I tried to pass the Me or Activity parameter in the initialize as well.
B4X:
Public Sub Initialize(Ac as object, EventName As String, AppKey As String, testMode As Boolean)
but i got crash on
B4X:
 StartAppSDK.RunMethod("init", Array(Ac,AppKey,True))

it only work with a context.
what would be the correct way to use the activity in which it was started, how to pass the context of the current activity to the SDK?

THXX
 

Douglas Farias

Expert
Licensed User
Longtime User
Hmm.
I see the problem now, it is on subexist or callsub, i dont know why.
B4X:
If SubExists(ctxt, EventGeneral) Then CallSub2(ctxt,EventGeneral, "onReceiveAd")

what should i use instead of ctxt?
I intend to make a callback to the main.

Here is the error log, using the code above.
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 38 (interstitialStartApp)
java.lang.ClassCastException: teste.app.iddouglasqw.main cannot be cast to java.lang.String
at anywheresoftware.b4a.keywords.Common.getComponentBA(Common.java:1218)
at anywheresoftware.b4a.keywords.Common.SubExists(Common.java:1007)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA$1.run(BA.java:352)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

thx
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why limit yourself to Main?

B4X:
Sub Class_Globals
    Private StartAppSDK As JavaObject
    Private StartAppAd As JavaObject
    Private ctxt As JavaObject
    Public isReady As Boolean = False
    Private AdListenerInterstitial As Object
    Private AdDisplayListener As Object
    Private mCallback As Object
    Private mEventName As String
End Sub


Public Sub Initialize(Callback As Object, EventName As String, AppKey As String, testMode As Boolean)
  mCallback = Callback
  mEventName = EventName

if XUI.SubExists(mCallback, mEventName & "_OnReceiveAd", 1) Then
  CallSub2(mCallback, mEventName & "_OnReceiveAd", "strange parameter, doesn't look like you need it")

'Main code
Dim ad As YourClass
ad.Initialize(Me, "Ad", "key", False)
 
Top