Android Question permission.WRITE_SETTINGS

Erel

Administrator
Staff member
Licensed User
Please don't post screenshots. Post the error message from the logs and post the relevant code.
 

Almora

Active Member
Licensed User
I have added my error code. I could not add setting write permissions.

B4X:
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 **
 

Almora

Active Member
Licensed User
there are no dangerous permits.


B4X:
rp.CheckAndRequest(rp.
PERMISSION_
WRITE_SETTINGS)
is not found.
 

Semen Matusovskiy

Well-Known Member
Licensed User
As I understand, not so simple
If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with actionSettings.ACTION_MANAGE_WRITE_SETTINGS. The app can check whether it has this authorization by calling Settings.System.canWrite().
There are Java samples on stackoverflow
 

Almora

Active Member
Licensed User
B4X:
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.
 

Almora

Active Member
Licensed User
added code..

B4X:
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", False, False, True, False)
            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
 

Erel

Administrator
Staff member
Licensed User
B4X:
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", False, False, True, False)
       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.
 
Top