Java: How to Access Public Fields in an Object?

62 views
Skip to first unread message

mkraft

unread,
Apr 15, 2018, 8:18:59 PM4/15/18
to Tasker
Hi,

If a Java object has public fields, how can I access them?

For example, the NotificationManager.Policy class has public final integer fields like 'priorityCategories'. There is no get() method for these, just the public field.

To be clear, I have no difficulty getting the Policy object, but once I have it, I just cannot see how to reference the public fields. How do I fill in the Java Function action to return these values?

Thanks,
mkraft

Robert Ryan

unread,
Apr 15, 2018, 10:04:54 PM4/15/18
to tas...@googlegroups.com

I could be mistaken, but isn't the Policy object just a string? If I use the following task, I get the attached result. Is that what you were looking for?

        Test (72)
       
    A1: Java Function
        Return: (NotificationManager) nmgr
        Class Or Object: CONTEXT
        Function: getSystemService
        {Object} (String)
        Param: notification
       
    A2: Java Function
        Return: %policy
        Class Or Object: nmgr
        Function: getNotificationPolicy
       {android.app.NotificationManager$Policy} ()
       
    A3: AutoTools Dialog
        Dialog Type:  Ok
        Title Alignment: Left
        Text: %policy
        Text Size: 20
        Dim Background: true
        Top Margin: 16
        Bottom Margin: 16
        Bottom Buttons Top Margin: 16
        Bottom Buttons Bottom Margin: 16
        Cancelable: true
        Turn Screen On: true
        Timeout (Seconds): 60

Screenshot_20180415-195531.png

Kraft, Michael

unread,
Apr 15, 2018, 10:31:52 PM4/15/18
to tas...@googlegroups.com
Well, certainly I can call toString() on the object (which is implicitly what happens in your example), and parse out all the contents of that string to figure out what I need to know - but do I really have to do all that?
The Policy object has discrete, public fields, and I feel like I should be able to read them individually.
Is there really no solution other than toString()? What about other objects where toString() might not include the necessary information?

Thanks,
mkraft

--
You received this message because you are subscribed to a topic in the Google Groups "Tasker" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/tasker/_2shHTJp1KE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to tasker+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/tasker.
For more options, visit https://groups.google.com/d/optout.

Robert Ryan

unread,
Apr 15, 2018, 11:52:14 PM4/15/18
to tas...@googlegroups.com

Oh, I see. To get the value of the field you can use the standard Java way of object.field as shown in step A3 below. Hope this helps.

        Test (72)
       
    A1: Java Function
        Return: (NotificationManager) nmgr
        Class Or Object: CONTEXT
        Function: getSystemService
        {Object} (String)
        Param: notification
       
    A2: Java Function

        Return: policy

        Class Or Object: nmgr
        Function: getNotificationPolicy
        {android.app.NotificationManager$Policy} ()
       

    A3: Java Function
        Return: %val
        Class Or Object: policy.priorityCategories
        Function: assign
        {int} ()
       
    A4: Flash
        Text: %val
        Long: Off

Kraft, Michael

unread,
Apr 16, 2018, 11:18:50 AM4/16/18
to tas...@googlegroups.com
Very good, thanks.
mkraft

--
Reply all
Reply to author
Forward
0 new messages