Android Question How check read and write permision on starter before initialize db

hibrid0

Active Member
Licensed User
Hi I want to know how can I check permisions from the starter service or Wait for check all permissions.

I want to check permisision camera, fine location, read and write storage. But my crash.
All calls to the permissions are OK with exception of android.permission.WRITE_EXTERNAL_STORAGE and android.permission.READ_EXTERNAL_STORAGE.
My app crash when find the db file, before the permisions dialog show.
I test to set manual permission on Android settings and my app work fine the all other permissions work with problem.


My Manifest:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.basic4ppc.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>
<uses-feature android:name="android.hardware.location.gps"/>)
   
   
'AddApplicationText(<meta-data
'    android:name="com.google.android.gms.version"
'    android:value="@integer/google_play_services_version" />)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo")
AddPermission(android.permission.PERMISSION_ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)
AddPermission(android.permission.CAMERA)
AddPermission(android.permission.READ_EXTERNAL_STORAGE)
AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)

'AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)

SetApplicationAttribute(android:clearTaskOnLaunch, "true")
SetApplicationAttribute(android:largeHeap,"true")
SetApplicationAttribute(android:hardwareAccelerated, "true")
SetActivityAttribute(act_esquema, android:screenOrientation, "landscape")

'AddManifestText(
'<uses-permission
'  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
'  android:maxSdkVersion="18" />
')
'End of default text.

My Sub
B4X:
Sub verificar_permisos

    For Each permission As String In Array(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE, Starter.rp.PERMISSION_CAMERA, Starter.rp.PERMISSION_ACCESS_FINE_LOCATION, Starter.rp.PERMISSION_RECORD_AUDIO )
        Starter.rp.CheckAndRequest(permission)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
        If Result = False Then
            ToastMessageShow("No permission!", True)
'            Activity.Finish
            Return
        End If
    Next
    'we have permission!
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
       
    Else
        ' Credit for the following code to DonManfred
'        rs= Msgbox2("You have not given permission - I can not proceed"    ,"Permission Failure","Yes","Cancel","No",Null)
'        If rs=DialogResponse.POSITIVE Then
'        StopService(Starter)
'        Activity.Finish
        ExitApplication
        Return True ' RETURN TRUE to consume the Event---
    End If
End Sub

Part of my Starter

B4X:
If File.Exists(File.DirDefaultExternal, db_name) =True    Then
        functions.FileMove(File.DirDefaultExternal, db_name, Ruta_db, db_name)
        SQL1.Initialize(Ruta_db, db_name, False)
        Log("movemos DB anterior a la nueva ubicación")
    Else if File.Exists(Ruta_db, db_name)= False Then
        File.Copy(File.DirAssets, "db.db", Ruta_db, db_name)
        SQL1.Initialize(Ruta_db, db_name, False)
        Log("Copiamos DB del archivo de la APP a la nueva ubicación")
    Else
        'File.Copy(File.DirAssets, "db.db", File.DirDefaultExternal, db_name)
        Log("DB Inicializada en la ruta nueva")
        SQL1.Initialize(Ruta_db, db_name, False)
    End If

Error log:
B4X:
Error occurred on line: 489 (Starter)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference

Line 489 on starter
B4X:
rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
 

hibrid0

Active Member
Licensed User
Auto Solved.

On Main stop the starter service.
start runtime permissions
after all permissions get
start starter service
start the activity needed.
 
Upvote 0

DonManfred

Expert
Licensed User
Auto Solved
i don´t think this is the correct solution. You should never stop or start the starter service manually.
 
Upvote 0

hibrid0

Active Member
Licensed User
i don´t think this is the correct solution. You should never stop or start the starter service manually.
I set the main to check permissions, the first line is stop starter service.
Check permissions, if all is ok, start the service and the next activity+layaout.
For me is working ok.
But I accept suggestions.
 
Upvote 0
Top