Cómo iniciar la configuración del GPS en segundo plano llamando al servicio de atención al cliente
1 Heredar una clase del Servicio.
2 Crea el método startService().
3 Cree el método endService (), sobrecargue el método onCreate y el método onDestroy, y llame a startService y endService en estos dos métodos.
4 En startService, obtenga Context.LOCATION_SERVICE a través del método getSystemService.
5 Implementar una nueva clase basada en LocationListener. Los cuatro métodos onLocationChanged, onProviderDisabled, onProviderEnabled y onStatusChanged se sobrecargarán de forma predeterminada. Para el método onLocationChanged es como actualizamos los datos GPS más recientes. Generalmente nuestras operaciones solo necesitan ser procesadas aquí.
6 Llame al método requestLocationUpdates de LocationManager para activar la adquisición de datos GPS con regularidad. Nuestra operación final sobre la longitud y latitud obtenidas se puede implementar en la función onLocationChanged.
7 Finalmente, use el botón para iniciar el Servicio y detener el Servicio en nuestra Actividad.
El código es el siguiente:
paquete com.offbye.gpsservice;
importar android.app.Service;
importar android .content .Context;
importar android.content.Intent;
importar android.location.LocationListener;
importar android.location.LocationManager;
importar android.os.Binder;
importar android.os.IBinder;
importar android.util.Log;
GPSService de clase pública se extiende Servicio {
// 2000ms
final estático privado largo minTime = 2000;
// Distancia mínima de cambio 10m
final estático privado float minDistance = 10;
Etiqueta de cadena = this.toString();
LocationManager privado LocationManager;
LocationListener privado LocationListener;
IBinder final privado mBinder = new GPSServiceBinder();
public void startService() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = nuevo GPSServiceListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime, minDistance,
locationListener);
}
public void endService() {
if (locationManager != null && locationListener != null) {
locationManager.removeUpdates(locationListener);
} p>
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Código auxiliar de método generado automáticamente
devolver mBinder;
}