Skip to content

Commit

Permalink
Use GnssStatus for target SDK version 31 because GpsStatus is deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
mendhak committed Sep 29, 2024
1 parent b9a3568 commit 976ca60
Showing 1 changed file with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.BitmapFactory;
import android.location.GnssStatus;
import android.location.Location;
import android.location.LocationManager;
import android.os.*;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;
Expand Down Expand Up @@ -70,6 +73,7 @@ public class GpsLoggingService extends Service {
private LocationManager passiveLocationManager;
private LocationManager towerLocationManager;
private GeneralLocationListener gpsLocationListener;
private GnssStatus.Callback gnssStatusCallback;
private GeneralLocationListener towerLocationListener;
private GeneralLocationListener passiveLocationListener;
private NmeaLocationListener nmeaLocationListener;
Expand Down Expand Up @@ -590,6 +594,7 @@ private void startPassiveManager() {
}
}


/**
* Starts the location manager. There are two location managers - GPS and
* Cell Tower. This code determines which manager to request updates from
Expand All @@ -616,6 +621,32 @@ private void startGpsManager() {
towerLocationListener = new GeneralLocationListener(this, "CELL");
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
gnssStatusCallback = new GnssStatus.Callback() {
@Override
public void onStarted() {
super.onStarted();
}

@Override
public void onStopped() {
super.onStopped();
}

@Override
public void onFirstFix(int ttffMillis) {
super.onFirstFix(ttffMillis);
LOG.info("Time to first fix: {}ms", ttffMillis);
}

@Override
public void onSatelliteStatusChanged(@NonNull GnssStatus status) {
super.onSatelliteStatusChanged(status);
setSatelliteInfo(status.getSatelliteCount());
gpsLocationListener.satellitesUsedInFix = status.getSatelliteCount();
}
};
}


gpsLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Expand All @@ -627,7 +658,13 @@ private void startGpsManager() {
LOG.info("Requesting GPS location updates");
// gps satellite based
gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsLocationListener);
gpsLocationManager.addGpsStatusListener(gpsLocationListener);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
gpsLocationManager.registerGnssStatusCallback(gnssStatusCallback);
}
else {
gpsLocationManager.addGpsStatusListener(gpsLocationListener);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (nmeaLocationListener == null){
Expand Down Expand Up @@ -724,6 +761,12 @@ private void stopGpsManager() {
if (gpsLocationListener != null) {
LOG.debug("Removing gpsLocationManager updates");
gpsLocationManager.removeUpdates(gpsLocationListener);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && gnssStatusCallback != null) {
gpsLocationManager.unregisterGnssStatusCallback(gnssStatusCallback);
}
else {
gpsLocationManager.removeGpsStatusListener(gpsLocationListener);
}

Expand Down

0 comments on commit 976ca60

Please sign in to comment.