Android Tutorial GPS tutorial

Status
Not open for further replies.

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

This is a nice library but I'm having some trouble figuring out how to work it. In my main activity I used this coding in Activity_Create.

B4X:
    If Starter.GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(Starter.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    End If

This code starts up the GPS.

In the starter I use this coding in GPS_LocationChanged.

B4X:
Sub GPS_LocationChanged (Location1 As Location)
    CallSub2(Main, "LocationChanged", Location1)
End Sub

When I start my app for the first time the GPS turns on and GPS_LocationChanged inside the starter executes as expected calling "LocationChanged" in the Main activity. That in turn calls another subroutine in the Main activity which eventually calls StopGps.

After the GPS is turned off there is a call to StartGps in another part of the app and the GPS starts up. I was expecting GPS_LocationChanged to be called just like it did when I started up the app for the first time but that event never executes. I'm assuming I'm missing a step here.

Can you tell me the additional coding I need so GPS_LocationChanged can execute after the GPS is started?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example was updated:
1. The Activity_PermissionResult is handled with Wait For which is simpler.

2. The call to StartGPS and StopGPS is done with CallSubDelayed instead of CallSub. This is useful as there could be cases where the starter service is destroyed while the app is in the background. With CallSubDelayed the starter service will be restarted if needed.
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User

B4X:
        If servicio.GPS.GPSEnabled = False Then
            ToastMessageShow("Por favor hailita el dispositivo GPS" & CRLF & "Y presiona el botón regresar", True)
            StartActivity(servicio.GPS.LocationSettingsIntent)
        Else
            servicio.Permisos.CheckAndRequest(servicio.Permisos.PERMISSION_ACCESS_FINE_LOCATION)
            Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
            If Result Then CallSubDelayed(servicio,"StartGPS")
        End If

I do not understand why I get this error, the only thing I did was to place the previous code on a button in which a Service is started
 

Colin Evans

Active Member
Licensed User
Longtime User
Hi Erel could you explain when I run this example i a list which state they are all False and no Lat or Long returned, I don't get any errors regarding GPS being on or not and my apps that use GPS seem to all be working okay
 

Colin Evans

Active Member
Licensed User
Longtime User
Yes, initially I was inside but have tried outside with the same results, all False
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…