How to use reflection library?

Status
Not open for further replies.

Theera

Well-Known Member
Licensed User
Longtime User
Last edited:

Theera

Well-Known Member
Licensed User
Longtime User
Hi all,
I don't understand how to know that R.RunMethod2("setRotation","270","java.lang.float") ,I try to see in Android APIs,but I don't understand them.
 
Upvote 0

NFOBoy

Active Member
Licensed User
Longtime User
Try this

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim lblTest As Label
   Dim currentAngle As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
   'Activity.LoadLayout("Layout1")
   
   lblTest.Initialize("lblTest")
   Activity.AddView(lblTest, 100dip, 100dip, 200dip, 50dip)
   lblTest.Color = Colors.red
   currentAngle = 0
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub lblTest_Click
Dim r As Reflector
r.Target = lblTest
currentAngle = currentAngle + 30
r.RunMethod2("setRotation", currentAngle, "java.lang.float")
End Sub
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi NFOBoy,
Thank you for replies,but it isn't right. I mean that how to know for using the method and class. The group of expert men who know about Android APIs Developler. I don't know that Where do they come from in the AndroidAPIs' website?
 
Last edited:
Upvote 0

NFOBoy

Active Member
Licensed User
Longtime User
Theera,

not quite sure I understand. The demo as posted does exactly what you are looking for: using reflection to set the rotation on a label.

Now, if you are asking "how did I get that by looking at the API?" Then, that means, I did a review of the View in Android at View | Android Developers

In reviewing the appropriate entry for View I saw this:
android:rotationX setRotationX(float) rotation of the view around the x axis, in degrees.

Then, after reviewing how to pass a Float using a Reflector, I wrote the code.

The Reflector allows us to get to the API (in some instances, not all) that we can bring in/push out to our B4A code.

Is that what you are asking?

(and I am no expert, thanks much to others before who have helped me to get the basics down)
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi NFOBoy and everybody,
I'm understand this function R.RunMethod2("setRotation","270","java.lang.float") ,but I don't understand the word ("setRotation") and the word"java.lang.float" that they are from? The function is only example. I had ever seen many function which used Reflection Lib,but I don't understand to looking for them in Android APIs. My knowledge of English make me terrible.
This is another example which used Reflection Lib (I cut only part of all)
B4X:
Dim obj1 As Reflector
   obj1.Target = edt
   obj1.RunMethod2("setFocusable", "False", "java.lang.boolean")
Why do I use "java.lang.boolean"? why don't use "java.lang.object " or "android.view.view"? I see from this info.
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you look at the documentation (in your link) you see in the table:
| void | setFocusable(boolean focusable) |
or at the specific chapter.
public void setFocusable (boolean focusable)
void means no return value
setFocusable is the method name
(boolean focusable) means that the method expects the variable focusable and that the variable type is a boolean.

So in obj1.RunMethod2("setFocusable", "False", "java.lang.boolean")
"setFocusable" is the method name
"False" is the value of the transmitted variable
"java.lang.boolean" is the variable type.

Best regards.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Klaus,
I'm very glad to see you again. Please to expand explained me that Why do we must use the "java.lang.boolean" ?(In the documents have many packages,I couldn't to correctly selected) and the method which used,I could define myself? If it's not,where do I found it. Thank you.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Please to expand explained me that Why do we must use the "java.lang.boolean"
Because the "setFocusable" method needs a boolean variable.
And the reflection methods need to have the paramter type given, and in the case of "setFocusable" it's "java.lang.boolean".
You find the information in the link you posted.
In this example
public void setRotation (float rotaiton)
you should use
obj1.RunMethod2("setRotation", 90, "java.lang.float")
because the requested parameter type float.

Best regards.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Klaus,
This is my new link ,but I couldn't find the method ("setFocusable"). This is my problem. I 'm sorry if my English isn't clearly. In another hand,I couldn't find the method("setRotation")
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
In your post#6 you have
... I see from this info.
The link in info points to the help for the View object.
On this page you scroll down the page til you find the table Public Methods:
In this table you find all the methods for the View object.
Scroll down further til you find: setFocuasble click on the lick and you get the explanation for this method.
Same for setRotation.

Best regards.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Klaus,
I'm understand about using Reflection Library is
1. I must know package of Android APIs
2. I must know class of package for using
3. Search the method of the class

Is right? or you have another way addition
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Klaus,
from the new thread in forum

Could I do this belows?
B4X:
Dim obj1 as Reflector
obj1.Target=editText1
obj1.RunMethod2("SetBackgroundColor",Colors.red,"java.lang.int")

and/or Could I use these?

and another thread in forum,I think that could use the reflection lib,but I couldn't do it works
 
Last edited:
Upvote 0

NFOBoy

Active Member
Licensed User
Longtime User
Theera,

First, the reason you don't see the documentation on the API site, is that Reflector is a B4A thing. You can read up on it here Basic4android - Reflection

Second, your link to the Color is good. But not where you will find the documentation you are looking for, examine the View properties on that same website to find out the call that a Reflector will make to the View. ( setBackgroundColor is one of those by the way, as you have shown)

Third, the methods for colors are already part of the B4A class, and I'm not sure what you gain by setting them thru a Reflector.

Fourth, if you do want a specific example, ask away on here, there are plenty of people that can, and want to help. But it seems like you are asking "How" in the general sense, and that is a methodology that Klaus and I have shown, but there are plenty of other ways to skin this cat.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi NFOBoy,

Unfortunately, I have read from google translate. I don't understand English for yours. I only try the practice,it is correct in mine. Do you mind if I need you write to me in Thai language privately? (Google translate is not sure for Thai) I think that I will be more understand from Klaus' guide,if you don't interupt this. Klaus doesn't interested in my thread.
 
Last edited:
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
Hi Klaus,
Why do I only waiting for your answer? Because someone gave me only guide,someone gave me only the answers,but nobody who gave all of them and teach how to do in order to do it myself in the future. Please to come back again. I think this thread is more useful for everybody who isn't still understand to using the reflection lib.
 
Upvote 0

Theera

Well-Known Member
Licensed User
Longtime User
I'm only a man who need to learn more.

Hi NFOBoy,and Klaus,
I have read this thread again, I think that I already understand in post #6,I'm sorry that I 'm not good at English. Please give me up my badly words.

Best Regards
Theera
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry for answering somewhat late, but I was absent a whole week.
Using the Reflection library needs to acquire knowledge on Android and it is not that easy and I am not an expert on this subject.
One way to learn is to look at examples you find on the forum and try to understand by testing (that's what I do).
You find the the Android documentation here.

Best regards.
 
Last edited:
Upvote 0

gawie007

Member
Licensed User
Longtime User
Hi,
This is a lot easier when the object is known to the user (for example a label / button / activity etc.)
I am trying to access the volume settings for notifications etc. since I want to bypass the "Blocking Mode" which could have turned down the volume on the ringer / notifications etc.
These values cannot be changed via the phone object (ie. Dim ph as Phone ... ph.getVolume)
In Android developers they say these settings should be set using the AudioManager and the only way to access these is via reflection since we have no AudioManager object available to us.
I have tried this little bit of simple code but just can't figure it out:

B4X:
Sub CheckNotificationSetting
    Dim RingerMode As Int
    Dim Target As Object
    Dim r As Reflector
 
    r.Target = "android.media.AudioManager"
    Log("r.Target: " & r.Target)
    RingerMode = r.RunMethod("getRingerMode")
    'RingerMode = r.RunStaticMethod("android.media.AudioManager","getRingerMode",Null,Null)
    Log("RingerMode in CheckNotificationSetting: " & RingerMode)
 
End Sub
Log("r.Target: " & r.Target) Logs: "android.media.AudioManager"
RingerMode = r.RunMethod("getRingerMode") produces: java.lang.NoSuchMethodException: getRingerMode() []
I am sure there is a simple solution to this, but not at the moment for my simple brain!
 
Upvote 0
Status
Not open for further replies.
Top