Android Question SOLVED - SetBrightnessToNormal crashes on Android 11

Robert Valentino

Well-Known Member
Licensed User
Longtime User
On my new Samsung S20 FE Android 11 API 30 this code crashes WORK fine on my Samsung Tablet running Android 10 API 29

I have called CanWriteToSystemSettings before trying SetBrightnessToNormalMode

B4X:
Private Sub SetBrightnessToNormalMode
               Dim jo         As JavaObject
               Dim System     As JavaObject
           
               jo.InitializeContext
               System.InitializeStatic("android.provider.Settings.System")
===> This line crashes              System.RunMethod("putInt", Array(jo.RunMethod("getContentResolver", Null), System.GetField("SCREEN_BRIGHTNESS_MODE"), System.GetField("SCREEN_BRIGHTNESS_MODE_MANUAL")))
End Sub

Private Sub CanWriteToSystemSettings As Boolean
               Dim p As Phone
           
               If     p.SdkVersion >= 23 Then
                   Dim ctxt As JavaObject
                   Dim settings As JavaObject
               
                   ctxt.InitializeContext
               
                settings.InitializeStatic("android.provider.Settings.System")
                   Return settings.RunMethod("canWrite", Array(ctxt))
               End If
  
            Log("Main::CanWriteToSystemSettings - True")
  
               Return True
End Sub

Is there a fix for Android 11 & API 30?



Crash
B4X:
*** Service (starter) Create ***
Main::Permissions  android.permission.WRITE_EXTERNAL_STORAGE
Main::Permissions  android.permission.READ_EXTERNAL_STORAGE
Main::Permissions  android.permission.READ_PHONE_STATE
Main::Permissions  android.permission.ACCESS_COARSE_LOCATION
Main::Permissions  android.permission.RECORD_AUDIO
*** Service (sbackground) Create ***
** Service (sbackground) Start **
*** Service (sdsdtechrelay) Create ***
** Service (sdsdtechrelay) Start **
*** Service (sipcontrol) Create ***
IPControl::ListenForConnections
IPControl::Service_Create - WiFiIP:127.0.0.1  IP:100.78.162.50
** Service (sipcontrol) Start **
*** Service (sipcontrolcmds) Create ***
** Service (sipcontrolcmds) Start **
*** Service (smymediabrowserservice) Create ***
Error occurred on line: 636 (Main)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:262)
    at com.BOBs.MusicDB.main._setbrightnesstonormalmode(main.java:6823)
    at com.BOBs.MusicDB.main._setdimming(main.java:3899)
    at com.BOBs.MusicDB.main$ResumableSub_StarterReady.resume(main.java:8242)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resumeAsUserSub(DebugResumableSub.java:48)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$DelegatableResumableSub.resume(DebugResumableSub.java:43)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1764)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8430)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.SecurityException: com.BOBs.MusicDB was not granted  this permission: android.permission.WRITE_SETTINGS.
    at android.os.Parcel.createExceptionOrNull(Parcel.java:2385)
    at android.os.Parcel.createException(Parcel.java:2369)
    at android.os.Parcel.readException(Parcel.java:2352)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:190)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.call(ContentProviderNative.java:732)
    at android.provider.Settings$NameValueCache.putStringForUser(Settings.java:2794)
    at android.provider.Settings$System.putStringForUser(Settings.java:3422)
    at android.provider.Settings$System.putStringForUser(Settings.java:3406)
    at android.provider.Settings$System.putIntForUser(Settings.java:3553)
    at android.provider.Settings$System.putInt(Settings.java:3546)
    ... 33 more
Caused by: android.os.RemoteException: Remote stack trace:
    at android.provider.Settings.isCallingPackageAllowedToPerformAppOpsProtectedOperation(Settings.java:21026)
    at android.provider.Settings.checkAndNoteWriteSettingsOperation(Settings.java:20906)
    at com.android.providers.settings.SettingsProvider.mutateSystemSetting(SettingsProvider.java:2000)
    at com.android.providers.settings.SettingsProvider.insertSystemSetting(SettingsProvider.java:1952)
    at com.android.providers.settings.SettingsProvider.call(SettingsProvider.java:576)
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Figured it out.

Forgot to call this below routine CanWriteToSystemSettings was false
B4X:
           If     CanWriteToSystemSettings = False Then
                   Dim in As Intent
                
                   in.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS", "package:" & Application.PackageName)
                   StartActivity(in)
                
                   Wait For Activity_Resume
                
                   If     CanWriteToSystemSettings = False Then
                       Log("no permission...")
                       Return
                   End If
               End If

NOW all works fine.
 
Upvote 0
Top