Is there a GetScreenBrightness function?

rfresh

Well-Known Member
Licensed User
Longtime User
Is there a function to get the current screen brightness setting?

Thanks...
 

vb1992

Well-Known Member
Licensed User
Longtime User
you would have to rewrite this
B4X:
Sub WriteSetting(Setting As String, Value As Int)
    Dim r1 As Reflector
    Dim args(3) As Object
    Dim types(3) As String

    r1.Target = r1.GetContext
    
    args(0) = r1.RunMethod("getContentResolver")
    types(0) = "android.content.ContentResolver"
    args(1) = Setting
    types(1) = "java.lang.String"
    args(2) = Value
    types(2) = "java.lang.int"
    r1.RunStaticMethod("android.provider.Settings$System", "putInt", args, types) 
End Sub


with this:


int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Thanks...I took a look at trying to rewrite it but it's beyond my understanding...I looked at the Reflection docs but really don't know which method() I would need to make it work. Franky, I don't understand the Reflection doc very well.

Since it's a simple function call, I thought the RunMethod would work but I don't know what argument is needed in that method. I'm not even sure RunMethod is the right method that needs to be used.

Here is what I have below but it doesn't work. I'm guessing at the RunMethod argument "getScreenBrightness" because I don't know what it should be. I also tried "Screen_Brightness" but that didn't work.

I can't figure out how to convert your android example into what B4A needs.

B4X:
Sub GetBrightness() As Int
   Dim r As Reflector
   r.Target = r.RunStaticMethod("java.util.Locale", "getDefault", Null, Null)
   Return r.RunMethod("getScreenBrightness")
End Sub
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
It's more towards this code
but this needs a little help too
to get it working :sign0085:

B4X:
   Dim r1 As Reflector
    Dim args(3) As Object
    Dim types(3) As String

    r1.Target = r1.GetContext
    
    args(0) = r1.RunMethod("getContentResolver")
    types(0) = "android.content.ContentResolver"
    args(1) = "SCREEN_BRIGHTNESS" 
    types(1) = "java.lang.String"
    args(2) = -1
    types(2) = "java.lang.int"
    
   Dim BrightInt As Int
   BrightInt = r1.RunStaticMethod("android.provider.Settings$System", "getInt", args, types) 
   
   Msgbox(BrightInt,"bright")

from:


int UserBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,-1);
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Yes I have the WRITE_SETTINGS statement in my manifest.

I guess where I get lost is I don't understand what the args() array and the types() array are doing. I understand they are array structures and that they are being passed to the RunStaticMethod() but I don't understand or know why I would use the RunStaticMethod() vs RunMethod4() for example.

I see args(0) element is loaded with r1.RunMethod() but I don't understand why.

I think what's missing from the Reflection doc is a beginners tutorial on the structure of making Reflection calls. The doc shows all the objects and methods, etc. but doesn't explain the structure options. I see several structure options (RunMethod, Method1, Method2) but don't know why I would pick one over the other.
 
Last edited:
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I'm using the phone lib now to set the brightness but in the phone doc I only see this:

SetScreenBrightness (Value As Float)

How do you get the current screen brightness? Am I missing that function in the phone doc?

(I wouldn't mind learning how to do it using Reflection since understanding how to use that lib will come in handy in the future).

Thanks...
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I was looking at the Android SDK (see attachment) but I don't see how you get the current screen brightness from any of these calls?

But like vb1992 indicated, I did find another Android SDK reference to getting the current screen brightness:
B4X:
Int oldBrightness = Settings.System.getInt(getContext().getContentResolver(),Settings.System.SCREEN_BRIGHTNESS);

So, I guess it can be done...I'm still trying to figure out how.

@vb1992 you code sample line args(2) = -1 always returns this value -1. If I change the value, it returns that value.
 
Last edited:
Upvote 0
Top