Android Question help with run time permissions

sdixon

Member
Licensed User
Longtime User
I am building a gps app but a can't seem to get the run time permissions right. Is there a best practice in how to use them?

I added this to the Activity_Create

B4X:
rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)

and this to the manifest

B4X:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

in the Activity_PermissionResult

B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_ACCESS_COARSE_LOCATION Then
        Result = True
  End If
End Sub

but when it goes through the Activity_resume where this code is placed

B4X:
If (Starter.nfpGPS.GPSEnabled) Then   
        Starter.nfpGPS.Start(0, 0) 'Listen to GPS with no filters.                     
  Else
    ToastMessageShow(Starter.nfpEnableGPS, True)
    StartActivity(Starter.nfpGPS.LocationSettingsIntent) 'Will open the relevant settings screen.       
  End If

I get an error telling me that I need the android.permission.ACCESS_FINE_LOCATION

I am obviously doing something wrong, but I can't figure it out.

Thanks
 

sdixon

Member
Licensed User
Longtime User
Sorry, it checked for both and I deleted the wrong one.

Having said that, and changing it I get the following error message when it hits this line of code

B4X:
Starter.nfpGPS.Start(0, 0) 'Listen to GPS with no filters.

B4X:
Installing file.
PackageAdded: package:com.unicornsystemz.perfect_parking
Copying updated assets files (33)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Error occurred on line: 181 (Main)
java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.
    at android.os.Parcel.readException(Parcel.java:1620)
    at android.os.Parcel.readException(Parcel.java:1573)
    at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:688)
    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:908)
    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:469)
    at anywheresoftware.b4a.gps.GPS.Start(GPS.java:117)
    at com.unicornsystemz.perfect_parking.main._activity_resume(main.java:795)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:703)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:340)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at com.unicornsystemz.perfect_parking.main.afterFirstLayout(main.java:103)
    at com.unicornsystemz.perfect_parking.main.access$000(main.java:18)
    at com.unicornsystemz.perfect_parking.main$WaitForLayout.run(main.java:81)
    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:7229)
    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)

Thanks
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sorry, it checked for both and I deleted the wrong one.
looks like you are still using the COARSE permisson, not the FINE permission (which is expected)
 
Upvote 0

sdixon

Member
Licensed User
Longtime User
I followed the updated tutorial to the letter but now I get the following.

When I start the app for the first time everything loads but when it goes to check the permission

Screenshot_20160623-093930.png with a java error of

B4X:
Installing file.
PackageAdded: package:com.unicornsystemz.perfect_parking
Copying updated assets files (33)
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
java.lang.RuntimeException: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
    at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:196)
    at com.google.android.gms.maps.MapFragment$zza$1.zza(Unknown Source)
    at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
    at android.os.Binder.transact(Binder.java:387)
    at wv.a(:com.google.android.gms.DynamiteModulesB:82)
    at maps.ag.t$5.run(Unknown Source)
    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:7229)
    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)
Caused by: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
    at maps.ag.t.c(Unknown Source)
    at uz.onTransact(:com.google.android.gms.DynamiteModulesB:274)
    at android.os.Binder.transact(Binder.java:387)
    at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.setMyLocationEnabled(Unknown Source)
    at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(Unknown Source)
    at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:183)
    ... 12 more

the problem seems to be in at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:196)

when I click away the app has stopped dialog I get the following, which I expect

Screenshot_20160623-093939.png and if I accept the permission, the app starts up as designed, but it would be nice if it started up right the first time.

I have made the changes to the manifest and added

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

Thanks
 
Upvote 0

sdixon

Member
Licensed User
Longtime User
Thanks for all your help.

B4X:
at anywheresoftware.b4a.objects.MapFragmentWrapper$1.onMapReady(MapFragmentWrapper.java:196)

I put the mapFragment in question into it's own layout, which was then loaded after the permissions were sorted out

B4X:
Sub Activity_PermissionResult (Permission As String, Result As Boolean)
  If Permission = Starter.rp.PERMISSION_ACCESS_FINE_LOCATION Then
      If Result Then
            CallSub(Starter, "StartGPS")
            Init_GoogleMaps
        End If
    End If
End Sub

and then everything works as designed. :)
 
Upvote 0
Top