Android Question permission.WRITE_SETTINGS

Discussion in 'Android Questions' started by Almora, Jun 20, 2018.

  1. Almora

    Almora Active Member Licensed User

    HELLO THERE.


    I want to make a ringtone. Requires write_settings permission. but I could not find this consent.
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    Please don't post screenshots. Post the error message from the logs and post the relevant code.
     
  3. Almora

    Almora Active Member Licensed User

    I have added my error code. I could not add setting write permissions.

    Code:
    audi_button2_click (java line: 740)
    java.lang.SecurityException: com.and.audi was 
    not granted  this permission: android.permission.WRITE_SETTINGS.
        at android.os.Parcel.readException(Parcel.java:
    1620)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:
    183)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:
    135)
        at android.content.ContentProviderProxy.call(ContentProviderNative.java:
    646)
        at android.provider.Settings$NameValueCache.putStringForUser(
    Settings.java:1457)
        at android.provider.Settings$System.putStringForUser(
    Settings.java:1852)
        at android.media.RingtoneManager.setActualDefaultRingtoneUri(
    RingtoneManager.java:1234)
        at android.media.RingtoneManager.setActualDefaultRingtoneUri(
    RingtoneManager.java:1174)
        at anywheresoftware.b4a.phone.RingtoneManagerWrapper.SetDefault(RingtoneManagerWrapper.java:
    98)
        at com.and.audi._button2_click(audi.java:
    740)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    191)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    175)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    171)
        at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:80)
        at android.view.View.performClick(
    View.java:5697)
        at android.widget.TextView.performClick(
    TextView.java:10826)
        at android.view.View$PerformClick.run(
    View.java:22526)
        at android.os.Handler.handleCallback(Handler.java:
    739)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    158)
        at android.app.ActivityThread.main(ActivityThread.java:
    7225)
        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)
    java.lang.SecurityException: com.and.audi was 
    not granted  this permission: android.permission.WRITE_SETTINGS.
    ** 
    Service (widget2x1) Start **
     
  4. DonManfred

    DonManfred Expert Licensed User

    The Constant Value is
    "android.permission.WRITE_SETTINGS"
     
  5. Almora

    Almora Active Member Licensed User

    there are no dangerous permits.


    Code:
    rp.CheckAndRequest(rp.
    PERMISSION_
    WRITE_SETTINGS)
    is not found.
     
  6. DonManfred

    DonManfred Expert Licensed User

    Use the constant
     
  7. Almora

    Almora Active Member Licensed User

    I did not understand:(
     
  8. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    As I understand, not so simple
    There are Java samples on stackoverflow
     
  9. DonManfred

    DonManfred Expert Licensed User

    Code:
    rp.CheckAndRequest("android.permission.WRITE_SETTINGS")
     
  10. Almora

    Almora Active Member Licensed User

    Code:
    rp.CheckAndRequest("android.permission.WRITE_SETTINGS")

    Wait for Activity_PermissionResult (Permission As String, Result As Boolean)

    if Result= False Then
    Msgbox ("false","")
    Else
    Msgbox ("true","")
    End if
    does not want permission. always returns false.
    ..permission manifest attached to the editor.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    It is not a regular permission. As @Semen Matusovskiy wrote it requires special handling. However I'm still waiting to see the relevant code...
     
  12. Almora

    Almora Active Member Licensed User

    added code..

    Code:
    Sub Process_Globals
        
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
         Private rp As RuntimePermissions
     
    End Sub

    Sub Globals
        
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    'Do not forget to load the layout file created with the visual designer. For example:
        
        
    Activity.LoadLayout("Layout1")
        
    'rp.CheckAndRequest("android.permission.WRITE_SETTINGS")
       
     rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        
    If Result=False Then
            
    Msgbox("false.." ,"")
        
    End If
        
                
        
    If File.Exists(File.DirRootExternal,"audi")=False Then
            
    File.MakeDir(File.DirRootExternal,"audi")
        
    End If
     

     
    End Sub

     

    Sub Activity_Resume
     
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub


    Sub Button1_Click
        

            
        
    If Not(rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
            
    Msgbox("No permission.." ,"")
            
        
    Else
                
            
    Dim r As RingtoneManager
            
    Dim u As String
        
     
            
    If File.Exists(File.DirRootExternal&"/audi""1.mp3") = False Then
                
    File.Copy(File.DirAssets, "1.mp3"File.DirRootExternal&"/audi""1.mp3")
            
    End If
            
    Try
                u = r.AddToMediaStore(
    File.DirRootExternal&"/audi""1.mp3""1.mp3"FalseFalseTrueFalse)
                
    File.WriteString(File.DirInternal, "ur1.txt", u)
            
    Catch
                u = 
    File.ReadString(File.DirInternal, "ur1.txt")
            
    End Try
            r.SetDefault(r.TYPE_RINGTONE, u)
     
        
    End If
     
    End Sub
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    I will write a small example next week.
     
    Almora likes this.
  14. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Sub Activity_Click
       rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
       
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
       
    If Result Then
           
    Dim dir As String = File.DirRootExternal
               
           
    Dim r As RingtoneManager
           
    Dim u As String
           
    File.Copy(File.DirAssets, "rows_completed.mp3", dir, "3.mp3")
           
    'you must handle the case where this file was already added. It will throw an error that you need to catch.
           u = r.AddToMediaStore(dir, "3.mp3""3.mp3"FalseFalseTrueFalse)
           
    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
               
       
    End If
       
    Log("setting default ringtone")
       r.SetDefault(r.TYPE_RINGTONE, u)
    End Sub

    Sub CanWriteToSystemSettings As Boolean
       
    Dim p As Phone
       
    If p.SdkVersion >= 23 Then
           
    Dim ctxt As JavaObject
           ctxt.InitializeContext
           
    Dim settings As JavaObject
           
    settings.InitializeStatic("android.provider.Settings.System")
           
    Return settings.RunMethod("canWrite"Array(ctxt))
       
    End If
       
    Return True
    End Sub
    BTW, you shouldn't use Msgbox. Only the async versions.
     
    lemonisdead, Kwame Twum and Almora like 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