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:

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
*** Service (screenshieldsrv) Create ***
** Service (screenshieldsrv) Start **
No wakelock.
** Activity (settings) Create, isFirst = true **
Panel size is unknown. Layout may not be loaded correctly.
** Activity (settings) Resume **
** Activity (settings) Pause, UserClosed = true **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
Error occurred on line: 182 (ScreenShieldSrv)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at anywheresoftware.b4a.phone.Phone.SetScreenBrightness(Phone.java:186)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
at www.sagital.scrshield.screenshieldsrv._updatemypnl(screenshieldsrv.java:652)
at www.sagital.scrshield.screenshieldsrv._statusow_touch(screenshieldsrv.java:628)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runGoodChain(Shell.java:463)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:285)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.flm.overlaywdw.OverlayWindow$1.onTouch(SourceFile:75)
at android.view.View.dispatchTouchEvent(View.java:9987)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2828)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2499)
at android.view.View.dispatchPointerEvent(View.java:10228)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5344)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5180)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4620)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4673)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4639)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4781)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4647)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4838)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4620)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4673)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4639)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4647)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4620)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7306)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7184)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7145)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7416)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:323)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 

JordiCP

Well-Known Member
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
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

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

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top