Service is now listening for location updates.
This commit is contained in:
parent
0fdd577943
commit
fa32356a9c
|
|
@ -1,11 +1,21 @@
|
||||||
package com.proculite.logmylocation;
|
package com.proculite.logmylocation;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Intent;
|
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.os.IBinder;
|
||||||
import android.util.Log;
|
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();
|
private final String TAG = LocationLoggingService.class.getName();
|
||||||
|
|
||||||
public LocationLoggingService() {
|
public LocationLoggingService() {
|
||||||
|
|
@ -16,10 +26,14 @@ public class LocationLoggingService extends Service {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("MissingPermission")
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
Log.d(TAG, "Service started.");
|
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);
|
return super.onStartCommand(intent, flags, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,4 +48,32 @@ public class LocationLoggingService extends Service {
|
||||||
|
|
||||||
super.onCreate();
|
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