Android Question Change Brightness from a Service

ilan

Expert
Licensed User
Longtime User
hi

i try to change the brightness from a Service but i get a NullPointerException.

this is the code:

B4X:
phone1.SetScreenBrightness(0.1)

this is the log:

 

JordiCP

Expert
Licensed User
Longtime User
This has worked for me (brightness between 0 and 255). I run it from a service but tested it being called from Activity. I guess it should also work with the service standalone
B4X:
'From https://stackoverflow.com/questions/6708692/changing-the-screen-brightness-system-setting-android?noredirect=1&lq=1
'needs permission WRITE_SETTINGS
Sub screenBrightness( brightnessInt As Int)     '0..255

   '    Original code
   '    android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
   '   android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt);
   
   Dim J As JavaObject
   J.InitializeContext
   J.RunMethod("setBrightness",Array(J,brightnessInt))
End Sub

#if JAVA
import android.content.Context;
public void setBrightness(Context mContext, int brightnessInt){
   android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
   android.provider.Settings.System.putInt(mContext.getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS, brightnessInt);
}
#End If

Needs AddPermission("android.permission.WRITE_SETTINGS") in manifest
 
Upvote 0

ilan

Expert
Licensed User
Longtime User

thank you so much @JordiCP. you are awesome my friend !!
 
Upvote 0

pappicio

Active Member
Licensed User
Longtime User
Hi, is this code works also for oreo version?, I tryed but seems doesn't have effects on...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…