Android Question Set Volume- error

Discussion in 'Android Questions' started by kisoft, May 12, 2018.

  1. kisoft

    kisoft Active Member Licensed User

    Hi everyone

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

    Code:
    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")


    Code:
    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: May 12, 2018
  2. ronell

    ronell Well-Known Member Licensed User

    try to add runtime permission to access the volume settings
     
  3. kisoft

    kisoft Active Member Licensed User

    HI

    I added all from the list
    Code:
    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
     
  4. ronell

    ronell Well-Known Member Licensed User

    does it solve the problem?
     
  5. kisoft

    kisoft Active Member Licensed User

  6. Erel

    Erel Administrator Staff Member Licensed 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.
    Code:
    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:
    Code:
    AddPermission(android.permission.ACCESS_NOTIFICATION_POLICY)
     
    Last edited: Jun 8, 2018
    lemonisdead and BillMeyer like this.
  7. kisoft

    kisoft Active Member Licensed User

    Thank you for your answer. I will not check it today.
     
  8. kisoft

    kisoft Active Member Licensed User

    HI
    I had a problem on LG G3 (Android 6.0). The application was closed without showing an error.
    I changed to:
    Code:
    If p.SdkVersion >= 24 Then
    Works now, Lg G3 is skipped. It works very well on other phones.
    Thank you for your help.
     
  9. DonManfred

    DonManfred Expert Licensed User

    You should ALWAYS create a NEW Thread for any question you have.

    DO NOT post any new question to existing threads.
     
    MrKim likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice