Android Question How to convert the following code from reflection to JavaObject ?

avacondios

Active Member
Licensed User
Longtime User
Hi,

I want to convert the following code from Reflection to javaObject.

B4X:
Dim mReflector As Reflector
mReflector.Target = mReflector.GetActivity
mReflector.Target = mReflector.RunMethod("getWindowManager")
mReflector.Target = mReflector.RunMethod("getDefaultDisplay")

the problem is , how to store the activity on javaObject variable.
It is not working, if I use the following stamement : Dim mJO as JavaObject = Activity. For the rest of commands, I am receiving an exception that getWindowManager and getDefaultDisplay not found.

Thanks for your support
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Add these two subs to your code:
B4X:
Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

B4X:
Dim manager As JavaObject = GetContext.RunMethod("getWindowManager", null)
...
 
Upvote 0

avacondios

Active Member
Licensed User
Longtime User
Thanks Erel....

How can I get the active context from a service ? I have a service that I want to get the properties of default display like height, width, etc. Is it possible to get the active context without to execute the GetContext sub from the activity ?
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hi all,
I have the code using Reflector already as below,I would like to convert to use JavaObject. I tried more than hours,but not finished. Please help me.
B4X:
Private Sub GetResources As Object
   
    Dim r1 As Reflector
    r1.Target = r1.GetContext
    Return r1.RunMethod("getResources")
EndSub


My tried is below which is wrong.
B4X:
Private Sub GetResources As Object
    Dim jo As JavaObject
     jo.GetField("context") 
     Return jo.RunMethod("getResources",Null)
EndSub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your code is wrong as jo is not pointing to any object.

Add these two subs to your code:
B4X:
Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

And then you should use:
B4X:
Private Sub GetResources As JavaObject
 Return GetContext.RunMethodJO("getResources", Null)
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hi Erel,or Experts

1. What is "Me"? in the line?
B4X:
      Dim cls AsString = Me
2. In the same case which always concerned I can't use JavaObject library instead of below
B4X:
Private Sub GetDrawable(id As Int) As Object
    Dim r2 As Reflector
    r2.Target = GetResources    '<--- without it the reflector object will be null
    Return r2.RunMethod2("getDrawable", id, "java.lang.int")

My tried is wrong as below
B4X:
Private Sub GetDrawable(id As Int) As Object
  Dim jo2 As JavaObject=GetResources
        Return jo2.RunMethod("getDrawable",id)
EndSub


P.S. I do it in order to add in my website issue at 55 ,Please understand me and don't feel annoyed me.
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hi Erel,
After I test my smartphone, I 've error "Program paused on line 68 jo.InitializeStatic(cls)" Do I have error in coding?
 

Attachments

  • Test.zip
    9 KB · Views: 218
Upvote 0

Theera

Expert
Licensed User
Longtime User
Hi all,
My computer's harddisk is breaken down ,I must earn money to buy a new one. I'm so late. This is my error message from the log. B4A has no copy button,I capture as picture instead.

P.S. The sourcecode at post #9 from Erel's tutorial,I had changed a little to use JavaObject lib instead of Reflection lib.
 

Attachments

  • MyError.png
    34.7 KB · Views: 211
  • errorAVD.png
    110.6 KB · Views: 218
Upvote 0

Theera

Expert
Licensed User
Longtime User

Me is refered as button?
 
Last edited:
Upvote 0