B4A Library Reflection library

There will be lots to come in future versions of Basic4Android as Erel adds more of Androids capabilities. In the mean time there will be things that would be nice to have but are not actually implemented. This library will help to solve at least some of those requirements.

This is the Android equivalent of the Basic4ppc Door library that turned out to be so useful. There is a demo for the library that adds onFocus and onTouch events to views that don't implement them, accesses objects fields and runs methods.

A separate demo shows how to "Linkify" text so that URLs in the text become active hyperlinks.

EDIT:- Version 1.1 posted. See post #17 for details.

EDIT:- Version 1.2 posted. Bug fix. See post#19 for details.

EDIT:- Version 1.3 posted. See post#20 for details.

EDIT:- Version 1.4 posted. See post#21 for details.

EDIT:- Version 1.5 posted. See post #25 for details.

EDIT:- Version 1.6 posted. See post #31 for details.

EDIT:- Version 1.7 posted. See post #32 for details.

EDIT:- Version 1.8 posted. See post #33 for details.

EDIT:- Version 1.9 posted. See post #48 for details.

EDIT:- Version 2.0 posted. See post #51 for details.

EDIT:- Version 2.1 posted. See post #53 for details.

EDIT:- Version 2.2 posted for Basic4android 2.0 or later. Version 2.1 remains for earlier versions. See post #57 for details.

EDIT:- Informatix has produced Version 2.3 in post #317

EDIT:- Version 2.4 posted. See post #91 for details.
 

Attachments

  • Reflection2.1.zip
    33.2 KB · Views: 1,912
  • Reflection2.4.zip
    33 KB · Views: 4,159
Last edited:

Mike Maurice

Member
Licensed User
Longtime User
Dim Obj1 As Reflector

Obj1.Target = Obj1.GetContext
Obj1.Target = Obj1.RunMethod2("getSystemService", "input_method", "java.lang.String")
Obj1.RunMethod("showInputMethodPicker")

Andrew Graham,
Assuming that the reflection library is making calls to the Android API and the arguments used in the B4A code are used to set up the calls.
I understand how the code above works, but am puzzled by the fact that there is no "input_method" string for an argument to getSystemService. There is an input_method_service string and if this latter is used, the code returns Null.

Do I assume correctly that your reflection library does not use the exact strings from the Android documentation for getSystemService?
If not, why not? How are users to know what the correct strings are if the library uses almost but not identical strings to the Android ones?
Is there documentation that explains these differences and if so where?
 

agraham

Expert
Licensed User
Longtime User
The library lets you manipulate Java objects using reflection and what you do and how you use it is entirely up to you. If you want to call getSystemService you need to use the strings as documented by Google. http://developer.android.com/refere...ntext.html#getSystemService(java.lang.String). In the above example you need to substitute "input_method" with the appropriate string from that documentation. "input_method" should return you an InputMethodManager for management of input methods that you can further manipulate.
 

Mike Maurice

Member
Licensed User
Longtime User
String INPUT_METHOD_SERVICE Use with getSystemService(String) to retrieve a InputMethodManager for accessing input methods.

This is the documentation from the page at the link in your previous post. There are about 50 or so strings near the top of the page, starting with ACCESSIBILITY_SERVICE and ending with WINDOW_SERVICE. There is NO string "input_method" on that page. There is a "input_method_service" string as I have just demonstrated immediately above.

It seems that either I am missing something or you are. My assumption originally was that possibly any one of these strings could be used as the second argument to the RunMethod2. If the string "input_method" is on that page, where is it?

For everybody's information and clarification.
INPUT_METHOD_SERVICE ("input_method")
An InputMethodManager for management of input methods.
This information is near the bottom of the page and for some reason did not show up in my "find" search on that string. In case this is still not clear. The strings that the reflection library expects are not the strings at the top of the page but those below the getSystemService section, and are not the ones in Blue, but the ones in the parenthesis and double quotes.

If I recall correctly, INPUT_METHOD_SERVICE is a C like macro replacement which equates to the string: input_method. All of this would probably be familiar to those who have been dabbling in low level android java.

Regards,
Mike
 
Last edited:

agraham

Expert
Licensed User
Longtime User
It seems that either I am missing something or you are.
You are, but I see you found it eventually which makes your aggressive attitude both unwelcome and unnecessary. I will ignore you in future.

The strings that the reflection library expects are not the strings at the top of the page but those below the getSystemService section, and are not the ones in Blue, but the ones in the parenthesis and double quotes.
The Reflection library expects nothing. As documented the call to getSystemService(String) expects a String so you need to give it one. What you refer to as "strings at the top" are not strings but are documenting the types of the static fields of Context that, in this case, return a String type.

If I recall correctly, INPUT_METHOD_SERVICE is a C like macro replacement which equates to the string: input_method.
Its actually a static field of the context class. Java does not support macros.

All of this would probably be familiar to those who have been dabbling in low level android java.
Such knowledge is a pre-requisite to successfully use reflection.
 

Chris Williams

Member
Licensed User
Longtime User
Hello, agraham. I have created an app which makes use of your library. Prior to me uploading it to google play, I would like to know what I need to do to give you proper credit for your work. I have included a credits page that is in the app...
 

Tayfur

Well-Known Member
Licensed User
Longtime User
hello @agraham

I want learn properties name for exlibrary.

I used come codes for check (maybe you have a diffrent way!!),
So , How can I get all properties name (get/set)

Obejet_name.Left
Obejet_name.Width
Obejet_name.Top
Obejet_name.Height

How can I get 4 property name... (Left,Width,top,height)

B4X:
'---THis sample  for property of "Max" 
Sub SubExistsReflection(o as Object, SubName as String, ArgTypes() as String) as Boolean
    Dim r as Reflector
    r.Target = o
    Try
        Dim method as Object = r.GetMethod(SubName, ArgTypes)
        Return True
     Catch
        Return False
     End Try

'-------------------

Log("SubExists11: " & SubExistsReflection(Panel_names.Get(i), "_setmax", Array As String("java.lang.int"))) ' returned TRUE
Log("SubExists21: " & SubExistsReflection(Panel_names.Get(i), "_setMax", Array As String("java.lang.int")))' returned FALSE
Log("SubExists31: " & SubExistsReflection(Panel_names.Get(i), "setmax", Array As String("java.lang.int")))' returned FALSE
Log("SubExists41: " & SubExistsReflection(Panel_names.Get(i), "setMax", Array As String("java.lang.int")))' returned FALSE
r.target=Panel_names.Get(i)
r.RunMethod2("_setmax", 100, "java.lang.int") '----->> its code work


End Sub
 
Top