Phone Display Brightness

vb1992

Well-Known Member
Licensed User
Longtime User
How do you get the phone brightness to stay set to your level
after you exit the app?

I set the brightness, then exit the app, and it goes back
to the screen brightness before i loaded the app?

Does this need to be run as a service?

thanks


Dim PH as Phone
Ph.SetScreenBrightness(.50)
 

Feri

Member
Licensed User
Longtime User
Change the global brightness from a service

This method only affects your application.

I don't think that there is an official API for globally setting the brightness. You can however use an intent to show the settings page.

Still is not any way to change the brightness globally from a service?
 
Upvote 0

Feri

Member
Licensed User
Longtime User
Android doesn't allow it.

There are lot of widgets on the Market which can adjust the brightness (switch between auto, or 25%-100%). Or there is an app (Brightness Battery Widget) which can adjust the brightness depending the battery level, and lot of "battery saver" apps can do this.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Android doesn't allow it.

You can set some System settings without problem. Some time ago I wrote a testing library for that and it works without problem to set the device brightness.

Setting screen brightness from a service is somewhat tricky because just changing the device settings does not really change the brightness. You still have to open a (invisible) dummy Activity and use Phone.SetScreenBrightness().
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
You can do it with Reflection. Use this Sub to set a Android Setting:

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

Your App needs the WRITE_SETTINGS permission to make changes, so add this in the Manifest Editor:

B4X:
AddPermission("android.permission.WRITE_SETTINGS")

Now you can change the brightness:

B4X:
Dim p As Phone
   
If Value > 15 Then
   WriteSetting("screen_brightness", Value)
   p.SetScreenBrightness(Value/255)
End If

You always have to call both:
WriteSetting to change the system setting (this does not affect current brightness)
Phone.SetScreenBrightness() to change current brightness.

Be aware that very small values switches the display completely off so the example only sets brightness, if Value is bigger than 15 (Value has a range from 0 to 255)

If you want to do this from a service like from a widget its much more tricky since you can't call Phone.SetScreenBrightness() from a service module. Then you have to startup an invisible activity (define transparent theme for it in manifest), call Phone.SetScreenBrightness() from this activity and exit the activity immediately.

For a list of settings you can change look here.

PS: Hey, I'm beginning to understand reflection. :sign0104:
 
Last edited:
Upvote 0

Feri

Member
Licensed User
Longtime User
Thanks, I will try it. Somewhere I read that this invisible activity must be open at least for 500ms to modify the brightness.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I'm a new B4A developer and I have two questions:

1. How and where do I add the WriteSetting() Sub?

2. What is Reflection?
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I looked at the Lib tab and didn't see Reflection in there so I assume I don't have to manually add it (it's built-in)?

I don't know how to add the Sub function into my current Activity for the WriteSetting? Do I need to create a new Code Module and add it there or can I add it to my existing Activity module?
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends

please for advice...
How to solve situation if I want use this code (from this thread) in service:
B4X:
Dim p AsPhone
If Value > 15Then
WriteSetting("screen_brightness", Value)
p.SetScreenBrightness(Value/255)
EndIf

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
I am using it in main aktivity. Everything is ok, but if I use it in service, the APP crashed.

I need use it in service.
It is possible?

Please can you show me right direction, how to solve it?
Thank you very much
p4ppc
 
Last edited:
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Erel, thank you,

In service module where I am using PHONEEVENTS (this is OK)
I am waiting to STATE="OFFHOOK" (this is OK)

after that in same service I change brightness with

B4X:
sub contrastplus
        Dim val As Int
        val=20
        Dim ph As Phone       
        WriteSettingg("screen_brightness", val)
        phk.SetScreenBrightness(val/255)
End sub

Sub WriteSettingg(Setting AsString, Value AsInt)
Dim r1 AsReflector
Dim args(3) AsObject
Dim types(3) AsString
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


and the app crashed with message: The app stop working

with this error message:



java.lang.NullPointerException

at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:185)
at aa.ap1.tuhile.myservice._contrastplus(audioservice.java:130)
at aa.ap1.tuhile.myservice._pe_phonestatechanged(audioservice.java:165)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler.send(PhoneEvents.java:326)
at anywheresoftware.b4a.phone.PhoneEvents$15.handle(PhoneEvents.java:245)
at anywheresoftware.b4a.phone.PhoneEvents$16.onReceive(PhoneEvents.java:268)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)

at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } in anywheresoftware.b4a.phone.PhoneEvents$16@4121c3f0
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:201)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
at anywheresoftware.b4a.phone.PhoneEvents$ActionHandler.send(PhoneEvents.java:326)
at anywheresoftware.b4a.phone.PhoneEvents$15.handle(PhoneEvents.java:245)
at anywheresoftware.b4a.phone.PhoneEvents$16.onReceive(PhoneEvents.java:268)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:758)
... 9 more
Caused by: java.lang.NullPointerException
at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:185)
at aa.ap1.tuhile.myservice._contrastplus(audioservice.java:130)
at aa.ap1.tuhile.myservice._pe_phonestatechanged(audioservice.java:165)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
... 14 more

Thank you very much
p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
I am heedless

In documentation is:
This method cannot be called from a service module.

Thank you Erel for save my time with finding the reason of my bug.
Best regards
p4ppc
 
Upvote 0
Top