Android Tutorial GPS tutorial

Status
Not open for further replies.
This example shows how to work with the GPS library.

upload_2016-6-23_9-56-49.png


The GPS object is declared in the Starter service. It is easier to put the GPS in a service and not in an Activity as the services life cycle is simpler.

When the program starts we check whether the location features are enabled:
B4X:
Sub Activity_Resume
   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)
       Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
       If Result Then CallSubDelayed(Starter, "StartGPS")
   End If
End Sub
If not then we open the relevant settings screen.

The next step is to check for the ACCESS_FINE_LOCATION permission (runtime permissions tutorial).
If we have permission then we call the StartGPS sub from the Starter service.

Now all that is left is to delegate the LocationChanged event from the service to the activity and show the information.
B4X:
'Starter service
Sub GPS_LocationChanged (Location1 As Location)
   CallSub2(Main, "LocationChanged", Location1)
End Sub

'Main activity
Public Sub LocationChanged(Location1 As Location)
   lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
   lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
   lblSpeed.Text = $"Speed = $1.2{Location1.Speed} m/s "$
End Sub

Due to Google policy change you also need to add this line to the manifest editor:
B4X:
AddManifestText(<uses-feature android:name="android.hardware.location.gps"/>)
This will prevent the app from being installed on devices without GPS hardware.

The example is attached.

Modified example with NMEA listener: https://www.b4x.com/android/forum/t...-not-firing-on-android-10.112140/#post-699351
 

Attachments

  • GPS.zip
    8.1 KB · Views: 4,993
Last edited:

rleiman

Well-Known Member
Licensed 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

Administrator
Staff member
Licensed 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
upload_2018-3-22_12-36-12.png

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
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
 
Status
Not open for further replies.
Top