Restart requestLocationUpdates() with Sleep

Asked

Viewed 347 times

2

How do I start the requestLocationUpdates() with Thread.sleep(); in the Android?

My code:

public void onLocationChanged(Location loc) {
    Date d = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

    SQLiteDatabase db = openOrCreateDatabase("pontos.db", Context.MODE_PRIVATE, null);       
    db.execSQL("INSERT INTO pontos(latitude, longitude, datahora) VALUES('"+ loc.getLatitude()+"','"+loc.getLongitude()+"','"+  sdf.format(d)+"')");

   db.close();
 Toast.makeText(getBaseContext(), "Atualizado", Toast.LENGTH_LONG).show();      
 locationManager.removeUpdates(this);   
  incremento();



}

private void incremento() {
     new Thread(new Runnable() {

           @Override
          public void run() {

               try {

                  Thread.sleep(5000);
                                      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new Servico());

               }catch(Exception e){}

                 }
            }).start(); 


}

I call the incremento(); at the end of onLocationChanged(), but nothing happens.
Detail: this is a service in Background.

  • It would be very interesting if you put some printStackTrace or even if it was a System.out.println inside the catch, because you are "swallowing" the exception. If there’s something wrong you won’t see what it was.

1 answer

1


How Locationmanager and Locationlistener work

The method LocationListener.onLocationChanged() is called whenever there is a new position obtained by GPS or triangulation antennas available (depending on whether you passed LocationManager.GPS_PROVIDER or LocationManager.NETWORK_PROVIDER for locationManager.requestLocationUpdates()).

The execution of onLocationChanged() does not depend on how many times it is called locationManager.requestLocationUpdates(); the latter only needs to be called once, and from there the method onLocationChanged() will be called again and again whenever a new position is available.

The method onLocationChanged() will always run on the main thread and, if memory serves, the method locationManager.requestLocationUpdates() should also be called from the main thread.

Pass this for him instead of new Servico(), and do not expect that it will produce positions each time it is called, that is, do not combine it with Thread.sleep() because that’s not how it works. Receiving positions will stop when you call locationManager.removeUpdates(this).

Suggested use

Start your position capture service by calling locationManager.requestLocationUpdates() and, as soon as it is called onLocationChanged(), call next locationManager.removeUpdates(this) to stop getting positions. Save the obtained position in the bank and schedule the next execution of the service for some time in the future through the class Alarmmanager.

This way the service will run once for each position capture. If you know in advance how much time you want to call the service, make this appointment via AlarmManager with the method setRepeating().

  • Excellent explanation Piovezan. you can tell me how to use Alarmmanager in my script ?

  • 1

    @Emersonbarcellos Take a look at this simple example: http://www.techrepublic.com/blog/software-engineer/use-androids-alarmmanager-to-schedule-an-event/

  • Which service should I schedule? all or only the locationManager.requestLocationUpdates()? sorry I’m new in java and thanks for understanding.

  • Ok already solved here, the tips helped me to solve. thank you very much Piovezan

Browser other questions tagged

You are not signed in. Login or sign up in order to post.