Android Question Application Crashes On First Time Permission Ask

A Z M JANNAT UL KARIM

Member
Licensed User
Hi, I had a service in Starter Module which will run after every 1 minute to get the current location. When the application first run it crashes on asking for permission. After allowing it next time it runs OK. I understand the the permission need to place first but how? Where did I do wrong ? Below is my code

In Main Activity Module

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If FirstTime Then
        Starter.reqForPerm.CheckAndRequest(Starter.reqForPerm.PERMISSION_SEND_SMS)
        Starter.reqForPerm.CheckAndRequest(Starter.reqForPerm.PERMISSION_ACCESS_FINE_LOCATION)
    End If
End Sub

In Starter Service

B4X:
Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Public LastLocation As Location
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    FusedLocationProvider1.Initialize("FusedLocationProvider1")
End Sub

Sub Service_Start (StartingIntent As Intent)
    StartServiceAt("", DateTime.Now + 30 * DateTime.TicksPerSecond, True) 'schedule the next task to run in 30 seconds.
    If Not(IsPaused("Starter")) Then
        If FusedLocationProvider1.IsConnected = False Then
            FusedLocationProvider1.Connect
        End If
    End If
End Sub

And the crash report below

FusedLocationProvider1_ConnectionSuccess
Error occurred on line: 159 (Starter)
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
at android.os.Parcel.readException(Parcel.java:1620)
at android.os.Parcel.readException(Parcel.java:1573)
at com.google.android.gms.internal.zzed.zzb(Unknown Source)
at com.google.android.gms.internal.zzcda.zza(Unknown Source)
at com.google.android.gms.internal.zzcdd.zza(Unknown Source)
at com.google.android.gms.internal.zzcdj.zza(Unknown Source)
at com.google.android.gms.internal.zzccc.zza(Unknown Source)
at com.google.android.gms.internal.zzbay.zzb(Unknown Source)
at com.google.android.gms.internal.zzbca.zze(Unknown Source)
at com.google.android.gms.internal.zzbcx.zze(Unknown Source)
at com.google.android.gms.internal.zzbcp.zze(Unknown Source)
at com.google.android.gms.internal.zzccb.requestLocationUpdates(Unknown Source)
at uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProviderWrapper.RequestLocationUpdates(FusedLocationProviderWrapper.java:202)
at com.bmc.coreBMCApp.starter._fusedlocationprovider1_connectionsuccess(starter.java:380)
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.raiseEvent(BA.java:166)
at uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProvider.onConnected(FusedLocationProvider.java:64)
at com.google.android.gms.common.internal.zzac.zzn(Unknown Source)
at com.google.android.gms.internal.zzbcp.zzm(Unknown Source)
at com.google.android.gms.internal.zzbcd.zzpY(Unknown Source)
at com.google.android.gms.internal.zzbcd.onConnected(Unknown Source)
at com.google.android.gms.internal.zzbcx.onConnected(Unknown Source)
at com.google.android.gms.internal.zzbbi.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzaa.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzn.zzrj(Unknown Source)
at com.google.android.gms.common.internal.zze.zzs(Unknown Source)
at com.google.android.gms.common.internal.zzi.zzrk(Unknown Source)
at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:231)
at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:200)
at anywheresoftware.b4a.keywords.Common.DoEvents(Common.java:399)
at com.bmc.coreBMCApp.main._ping(main.java:1251)
at com.bmc.coreBMCApp.main._getdevstat(main.java:1106)
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 com.bmc.coreBMCApp.main.afterFirstLayout(main.java:102)
at com.bmc.coreBMCApp.main.access$000(main.java:17)
at com.bmc.coreBMCApp.main$WaitForLayout.run(main.java:80)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Thank you in advance ...
 

BillMeyer

Well-Known Member
Licensed User
Longtime User
I had a similar problem and then decided to change to the following way: (If it is correct or not, or just a dirty fix - I don't know)

Library: RunTimePermissions v1.20

B4X:
    #AdditionalJar: com.google.android.gms:play-services-maps
    #AdditionalJar: core-3.2.1

    #DebuggerForceStandardAssets: true 
    #BridgeLogger: True


Sub Process_Globals
    Public Permissions As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)

    CheckPermissions ' I call this first regardless
    Activity.LoadLayout("scrMain") 'whatever you call your main layout
  
    End Sub
  
  
Public Sub CheckPermissions

'    Use Whatever permissions you require here - This is my set of permissions in one of my Apps
' Also CheckAndRequest - Checks - If not granted - then requests i.o.w's if it is the second time you are entering the App - it will check - if all is in place the user will not be requested to give permissions again.

    Log("Checking Permissions")
  
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_READ_EXTERNAL_STORAGE)
    Permissions.CheckAndRequest(Permissions.PERMISSION_WRITE_EXTERNAL_STORAGE)
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_READ_PHONE_STATE)
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_CALL_PHONE)
    Permissions.CheckAndRequest(Permissions.PERMISSION_RECEIVE_SMS)
    Permissions.CheckAndRequest(Permissions.PERMISSION_SEND_SMS)
    Permissions.CheckAndRequest(Permissions.PERMISSION_READ_SMS)
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_READ_CONTACTS)
    Permissions.CheckAndRequest(Permissions.PERMISSION_WRITE_CONTACTS)
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_ACCESS_COARSE_LOCATION)
    Permissions.CheckAndRequest(Permissions.PERMISSION_ACCESS_FINE_LOCATION)
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_CAMERA)
  
    Permissions.CheckAndRequest(Permissions.PERMISSION_READ_CALENDAR)
    Permissions.CheckAndRequest(Permissions.PERMISSION_WRITE_CALENDAR)

    CheckForGooglePlayServices
  
    StartService("HttpUtils2Service")
    StartService("FirebaseMessaging")
    StartService("sms1")
    StartService("NotificationService")

    Return
  
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)

 
    If Permission = Permissions.PERMISSION_ACCESS_FINE_LOCATION Then
        CallSubDelayed(Starter,"StartGps")
'       only after you have permission then you start the GPS or FusedLocation Provider in your Starter Service
    End If

    If Permission = Permissions.PERMISSION_CAMERA Then
        LogColor("YOU CAN USE THE CAMERA",Colors.Red)
    End If
  
    If Permission = Permissions.PERMISSION_READ_EXTERNAL_STORAGE Or Permission = Permissions.PERMISSION_WRITE_EXTERNAL_STORAGE Then
        LogColor("Read/Write Granted",Colors.Red)
    End If

    Log("Permissions OK")

End Sub

Sub CheckForGooglePlayServices As Boolean
    Log("Checking for Play Services")
    Dim GoogleApiAvailablity As JavaObject
    GoogleApiAvailablity = GoogleApiAvailablity.InitializeStatic("com.google.android.gms.common.GoogleApiAvailability").RunMethod("getInstance", Null)
    Dim context As JavaObject
    context.InitializeContext
    If GoogleApiAvailablity.RunMethod("isGooglePlayServicesAvailable", Array(context)) <> 0 Then
        GoogleApiAvailablity.RunMethod("makeGooglePlayServicesAvailable", Array(context))
        Return False
    End If
    Return True
End Sub

I trust this will help a little in solving your problem.
 
Upvote 0

A Z M JANNAT UL KARIM

Member
Licensed User
Erel, I had already published the app to the Play Store. Now it is not making me to downgrade the SDK :-( Can you please give an example for how to wait for Permission Approval. Thank You ...
 
Upvote 0
Top