requestLocationUpdates of LocationManager

1

I have a question, I hope you can help me.

I am using the requestLocationUpdates method that returns data taken by GPS every so often. My goal is that this data is sent to a server, even when the application is closed.

I have the idea that if I send it in the onCreate of the application I would not have problems with overloading the main thread, since this would be executed only once, but I do not know what the lifetime of the method would be.

What is the good practice in this type of development?

I leave my code in onCreate, doing tests in the emulator this works (I do not know how efficiently with respect to memory), but only while the application is active or in the list of applications used lately, when removing it from that list it stops working until you re-launch the application.

public class PacienteApp extends Application {
@Override
public void onCreate() {
    try {
        LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Location loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc != null) {
            Log.i("location", String.valueOf(loc.getLongitude()));
            Log.i("location", String.valueOf(loc.getLatitude()));
        }
        LocationListener locListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                Log.i("location", String.valueOf(location.getLongitude()));
                Log.i("location", String.valueOf(location.getLatitude()));
            }

            public void onProviderDisabled(String provider) {
                Log.i("info", "Provider OFF");
            }

            public void onProviderEnabled(String provider) {
                Log.i("info", "Provider ON");
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
                Log.i("LocAndroid", "Provider Status: " + status);
                Log.i("info", "Provider Status: " + status);
            }
        };
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 500, locListener);
    }
    catch(Exception e) {
        Log.e("ERROR", "Error: " + e);
    }
    finally {
        Log.i("INFO", "Salimos de onCreate");
    }
}
}
    
asked by manduinca 10.03.2016 в 22:30
source

1 answer

0

You can use a Service :

link

  

A service is an application component that you can perform   long-running operations in the background and does not provide a   user interface. This allows you to handle long operations   duration without affecting its user interface responsiveness.

I recommend this example:

link

Dentr of each cycle you can send the values of Latitude, Length to the server.

    
answered by 11.03.2016 / 00:19
source