Android Question [Solved] Correct way to get BA Context from within a class?

Status
Not open for further replies.

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
I'm using the "GetBA" method that @Erel has posted here in the forums several times to get the BA context in order to play a system sound effect. The method works fine in a class as long as the class' "caller" is an activity. If I instantiate the class playing the sound effect inside another class (which is instantiated from an activity) the GetBA method fails with a "class not found" exception.

I don't know enough about how the underlying Java works or how the BA process is structured to make an informed guess as to how to fix this. How can I modify the GetBA routine so it will work with a class hosted both on an Activity and from inside another class?

Play system sounds methods:
Private Sub PlayDefaultClick
    'moCaller is the "callback" object and may be an Activity or another class...
    If moCaller <> Null Then
        PlaySoundEffect(0) 'FX_KEY_CLICK
    End If
End Sub

Private Sub PlayKeypressStandard
    If moCaller <> Null Then
        PlaySoundEffect(5) 'FX_KEYPRESS_STANDARD
    End If
End Sub

Private Sub PlaySoundEffect(SoundEffectConstant As Int)
    Dim am As JavaObject = GetContext.RunMethod("getSystemService", Array("audio"))
    am.RunMethod("playSoundEffect", Array(SoundEffectConstant))
End Sub


Private Sub GetContext As JavaObject
    Return GetBA.GetField("context")
End Sub

Private Sub GetBA As JavaObject
    Dim jo As JavaObject
    Dim cls As String = moCaller
    cls = cls.SubString("class ".Length)
    jo.InitializeStatic(cls)  'fails with "class not found" exception when moCaller is another class.
    Return jo.GetFieldJO("processBA")
End Sub
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
call JavaObject.InitializeContext.
Thank you, @Erel, you are correct as always ;) One of these days I'm going to take the time to learn a little bit about Java and how it interacts. Changing the GetContext sub to the method below works fine no matter where the class is instantiated:
New GetContext method:
Private Sub GetContext As JavaObject
    Dim jo As JavaObject
    jo.InitializeContext
    Return jo
End Sub
 
Upvote 0
Status
Not open for further replies.
Top