Android Question get / set brightness

Discussion in 'Android Questions' started by yaniv hanya, Jul 22, 2019.

  1. yaniv hanya

    yaniv hanya Active Member Licensed User

    why when I use this code

    Code:
    Dim ref As Reflector
        ref.Target = ref.GetActivity
        ref.Target = ref.RunMethod(
    "getWindow")
        ref.Target = ref.RunMethod(
    "getAttributes")
        
    Dim brightness As Float = ref.GetField("screenBrightness")
    I get brightness = -1
    how can I get the real value?
     
  2. MarkusR

    MarkusR Well-Known Member Licensed User

  3. yaniv hanya

    yaniv hanya Active Member Licensed User

    yes i saw this but how can i get it out from the automatic state?
    I did not find it on the device settings
     
  4. MarkusR

    MarkusR Well-Known Member Licensed User

  5. yaniv hanya

    yaniv hanya Active Member Licensed User

    It really looks like what I'm looking for.
    Do you have any idea how I implement this in b4a?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You cannot get the brightness value if it is set to auto mode.

    You can set the brightness with Phone.SetScreenBrightness.
     
  7. MarkusR

    MarkusR Well-Known Member Licensed User

    first i would test it in android studio and if it works makeing a library for b4a then.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    The code to set the brightness mode to normal is:
    Code:
    Sub SetBrightnessToNormalMode
       
    Dim jo As JavaObject
       jo.InitializeContext
       
    Dim System As JavaObject
       System.InitializeStatic(
    "android.provider.Settings.System")
       System.RunMethod(
    "putInt"Array( _
           jo.RunMethod(
    "getContentResolver"Null), _
           System.GetField(
    "SCREEN_BRIGHTNESS_MODE"), _
           System.GetField(
    "SCREEN_BRIGHTNESS_MODE_MANUAL")))
    End Sub
    However this is not enough. You also need to request the special WRITE_SETTINGS permission.
    You can see an example here: https://www.b4x.com/android/forum/threads/permission-write_settings.94311/#post-597465
     
    MarkusR likes this.
  9. MarkusR

    MarkusR Well-Known Member Licensed User

    i made a test project, can't get the auto value as Erel said, only if set to manual :(
     

    Attached Files:

  10. yaniv hanya

    yaniv hanya Active Member Licensed User

    For some reason rp.CheckAndRequest("android.permission.WRITE_SETTINGS") did not open the permission dialogue on my device
    so i get this exception
    I noticed it in more permissions i was requesting
    Do you have any idea why?

    while runnig I get this log
    but nothing hapends
     
    Last edited: Jul 23, 2019
  11. MarkusR

    MarkusR Well-Known Member Licensed User

    i also noticed.
    i think because every user will agree all permissions (or u can not use most of all apps^^)
    google hide this settings. but u can open the os permissions dialog via intent where the user can enable it manual.
     
  12. yaniv hanya

    yaniv hanya Active Member Licensed User

    in the runtime permissions erel made and in the firs test i made for it few weeks ago it did opend.
    but since then they dont show.
    this is probably why the BrightnessTest.zip you posted befor don't work for me because the permission is false
    but why they do not open?
     
    Last edited: Jul 23, 2019
  13. Brandsum

    Brandsum Well-Known Member Licensed User

    I think you need to add this permission to your manifest. Then CheckAndRequest will pop a permission dialog.
     
  14. jimmyF

    jimmyF Active Member Licensed User

    Delete the app from the device and then re-install it.
     
  15. Erel

    Erel Administrator Staff Member Licensed User

  16. MarkusR

    MarkusR Well-Known Member Licensed User

    do you have any benefit from the sensor data itself?
    it is possible to read this sensor in app game kit with GetRawLightLevel so it should also possible in b4a.
     
  17. yaniv hanya

    yaniv hanya Active Member Licensed User

    Thanks friends
    With the help of your help, I succeeded in solving the issue.
    The dialogue has not yet opened to me, but I think it's a subject for new thread.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice