Android Question How to stop gps after getting coordinates ?

systems1

Member
Licensed User
Longtime User
Hello,
This is my service code which take GPS Coordinates every ten minutes . What I need is how can i stop the GPS after getting coordinates ?

Here I can see it is updating coordinates of each movements. I don't want to trace each movements, just current coordinates after that stop searching. and it will start searching after 10 minutes.

Also if I don't get the coordinates suppose I am inside home how can i stop searching ?


B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim GPS1 As GPS
    Dim TimerService As Timer
    Dim Counter As Int
    Counter=0
    Dim Notification1 As Notification
End Sub
Sub Service_Create
    TimerService.Initialize("TimerService",600000)   
      TimerService.Enabled=True   
End Sub

Sub Service_Start (StartingIntent As Intent)   
    If GPS1.IsInitialized = False Then
        GPS1.Initialize("GPS")
    End If
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent)
    Else
        GPS1.Start(0,0)
    End If
End Sub
Sub TimerService_Tick        
    If GPS1.IsInitialized = False Then
        GPS1.Initialize("GPS")
    End If
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent)
    Else
        GPS1.Start(0,0)
    End If   
End Sub
Sub Service_Destroy
    GPS1.Stop
End Sub
Sub GPS_LocationChanged (Location1 As Location)
      Main.SQL1.ExecNonQuery2("UPDATE settings SET latitude=?,longitude=?",Array As Object(Location1.Latitude,Location1.Longitude))   
End Sub


Please help me

Thanks and regards.
 

systems1

Member
Licensed User
Longtime User
How do you see that it doesn't stop? Maybe the event is still raised a few times because of existing messages in the message queue.
When the app look for coordinates I can see a small blinking satellite icon on the top of the device. I can see the same blinking satellite icon even if i close the app/stop the gps from app
 
Upvote 0

Berj

New Member
Licensed User
Longtime User
Hello Erel,
I have the same problem with gps service I use in my app.
I set the service to run every 3 minutes and log gps coordinates in a table.
I see too many rows in the table which means that "GPS2.Stop" does not actually stop the GPS.
I've noticed that this happens when the device is in movement (car).



B4X:
Sub GPS2_LocationChanged (Location1 As Location)
    If Location1.Accuracy<30 Then           
        Main.SQL1.ExecNonQuery("insert into gps_log(timest,latitude,longitude) values(datetime('now'),'"&Location1.Latitude&"','"&Location1.Longitude&"')")
        Main.g_lat=Location1.Latitude
        Main.g_long=Location1.Longitude
        Main.g_gps_last = DateTime.Date(DateTime.Now) &" "& DateTime.Time(DateTime.Now)

        Main.SQL1.ExecNonQuery("update gps set gps_last='"&Main.g_gps_last&"',latitude='"&Location1.Latitude&"',longitude='"&Location1.Longitude&"'")
        GPS2.Stop
       
        Log("1")
       
    End If
End Sub


i am starting the gps with following code
B4X:
    If GPS2.IsInitialized=False Then
        GPS2.Initialize("GPS2")
    End If
   
    If GPS2.GPSEnabled = False Then
        StartActivity(GPS2.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        GPS2.Start(500, 20) 'Listen to GPS with no filters.
    End If


Regards,
Berj
 
Upvote 0

Berj

New Member
Licensed User
Longtime User
I think I found where is my error. I have one more GPS object in my Main module... so i probably it blocks the normal work of service. It works fine when there is one GPS obeject.

It is still not working as i am expecting ... but i will try some more time.
Regards
 
Last edited:
Upvote 0

Alistair Sim

New Member
Licensed User
Longtime User
Hi Erel (and anybody else that might be able to help :)

Is my understanding correct that the GPS library can only return a location through the GPS_LocationChanged sub?
So then, if I want to see what the current location is, the user would in fact have to move at least once.
Is there maybe a library function that I'm missing somewhere that will give me the current position?
In the app that I'm writing I simply need to start the GPS object, get the location and set it to a keyvalue store and then stop the gps_object.

Kind Regards
Alistair Sim
 
Upvote 0
Top