Service is now listening for location updates.
This commit is contained in:
parent
0fdd577943
commit
fa32356a9c
|
|
@ -1,11 +1,21 @@
|
|||
package com.proculite.logmylocation;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.location.LocationProvider;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
public class LocationLoggingService extends Service {
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.location.LocationManagerCompat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LocationLoggingService extends Service implements LocationListener {
|
||||
private final String TAG = LocationLoggingService.class.getName();
|
||||
|
||||
public LocationLoggingService() {
|
||||
|
|
@ -16,10 +26,14 @@ public class LocationLoggingService extends Service {
|
|||
return null;
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.d(TAG, "Service started.");
|
||||
|
||||
LocationManager locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
|
||||
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
|
|
@ -34,4 +48,32 @@ public class LocationLoggingService extends Service {
|
|||
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged(@NonNull Location location) {
|
||||
Log.d(TAG, String.format(
|
||||
"New location. Latitude: %s Longitude: %s Altitude: %s",
|
||||
location.getLatitude(),
|
||||
location.getLongitude(),
|
||||
location.getAltitude())
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFlushComplete(int requestCode) {
|
||||
Log.d(TAG, "Flush complete.");
|
||||
LocationListener.super.onFlushComplete(requestCode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderEnabled(@NonNull String provider) {
|
||||
Log.d(TAG, "GPS provider became enabled.");
|
||||
LocationListener.super.onProviderEnabled(provider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProviderDisabled(@NonNull String provider) {
|
||||
Log.d(TAG, "GPS provider became disabled.");
|
||||
LocationListener.super.onProviderDisabled(provider);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue