Android Question Set Volume- error

kisoft

Well-Known Member
Licensed User
Longtime User
Hi everyone

I use this code to set the ringtone volume to the maximum value

B4X:
  Dim p As Phone
    '  p.SetMute(p.VOLUME_RING,False)
      p.SetVolume(p.VOLUME_RING, p.GetMaxVolume(p.VOLUME_RING), False)

Unfortunately, this error appears on Lg G6 ( SDK "26")


B4X:
Error occurred on line: 287 (Main)
java.lang.SecurityException: Not allowed to change Do Not Disturb state
    at android.os.Parcel.readException(Parcel.java:1683)
    at android.os.Parcel.readException(Parcel.java:1636)
    at android.media.IAudioService$Stub$Proxy.setStreamVolume(IAudioService.java:1053)
    at android.media.AudioManager.setStreamVolume(AudioManager.java:1130)
    at android.media.AudioManagerEx.setStreamVolume(AudioManagerEx.java:396)
    at anywheresoftware.b4a.phone.Phone.SetVolume(Phone.java:380)
    at finder.finder.main._startalarm(main.java:1566)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    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.BA$2.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6316)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

On the "19" SDK, the code worked well. This error only appears when the ringer is turned off. Can this blockade be bypassed?
 
Last edited:

kisoft

Well-Known Member
Licensed User
Longtime User
HI

I added all from the list
B4X:
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_RECEIVE_SMS)
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access RECIVE SMS", "")
        Return
    Else
        Log("OK")
    End If
    
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_SEND_SMS)
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access SEND SMS", "")
        Return
    Else
        Log("OK")
    End If
  
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)    '
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access lOCATION", "")
        Return
    Else
        Log("OK")
    End If
  
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)  '
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)
    If PResult = False Then
        MsgboxAsync("No permission to access PHONE STATE", "")
        Return
    Else
        Dim pid As PhoneId
        Log("phone id: " & pid)
    End If
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You have two options:

1. Catch this error and don't do anything. If the user choose to not be disturbed then it makes sense to respect it.

2.
B4X:
Sub ChangeVolume
   Dim p As Phone
   If p.SdkVersion >= 24 Then
       Dim NotificationManager As JavaObject
       NotificationManager = NotificationManager.InitializeContext.RunMethod("getSystemService", Array("notification"))
       If NotificationManager.RunMethod("isNotificationPolicyAccessGranted", Null) = False Then
           Dim in As Intent
           in.Initialize("android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS", "")
           Sleep(500) 'let the first Activity_Resume be handled. Important if this code is called from Activity_Create.
           StartActivity(in)
           Wait For Activity_Resume
           Log("after resume")
           If NotificationManager.RunMethod("isNotificationPolicyAccessGranted", Null) = False Then
               Log("no permission")
               Return
           End If
       End If

   End If
   p.SetVolume(p.VOLUME_RING, p.GetMaxVolume(p.VOLUME_RING), False)
End Sub

Manifest editor:
B4X:
AddPermission(android.permission.ACCESS_NOTIFICATION_POLICY)
 
Last edited:
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
HI
I had a problem on LG G3 (Android 6.0). The application was closed without showing an error.
I changed to:
B4X:
 If p.SdkVersion >= 24 Then
Works now, Lg G3 is skipped. It works very well on other phones.
Thank you for your help.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I am having a similar issue.
You should ALWAYS create a NEW Thread for any question you have.

DO NOT post any new question to existing threads.
 
Upvote 0
Top