I have been trying to receive location updates when my phone is locked I am using a service that is launched by my main activity. I receive location updates when the application is open just fine but when locking the phone the service continues to run but the location stored is the same every time.
import static com.example.kromby.mapsapplication.featureOptions.date;
public class BackgroundLocationUpdate extends Service implements GoogleApiClient.ConnectionCallbacks, LocationListener {
// I've removed code that does not relate to the question so please ignore most imports.
private GoogleApiClient mClient;
private boolean connected = false;
private LocationRequest mLocationRequest;
public static List<LatLng> globalLatLng = Collections.synchronizedList(new ArrayList<LatLng>());
public static GoogleMap mMap;
Location mLastLocation;
private static double latitude;
private static double longitude;
public LatLng mLatLng = new LatLng(latitude, longitude);
private static final String TAG = "MapActivity";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@TargetApi(Build.VERSION_CODES.O)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Runnable r = new Runnable() {
@Override
public void run() {
buildGoogleApiClient();
}
};
Runnable q = new Runnable() {
@Override
public void run() {
long p = System.currentTimeMillis() + 4000;
boolean continueLooping = true;
while (continueLooping) {
if (System.currentTimeMillis() >= p) {
locationUpdates();
continueLooping = false;
}
}
}
};
Thread mThread = new Thread(r);
Thread mThreadTwo = new Thread(q);
mThread.start();
mThreadTwo.start();
return Service.START_STICKY;
}
public void locationUpdates() {
long i = System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis() == i) {
i = System.currentTimeMillis() + 3000;
// Checks that the application has user permissions to location.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mClient);
// Once the phone is locked this will return the same location each time and not change.
Log.d(TAG, "Client is working " + mClient.isConnected() + "\n Current mLastLocation LatLng " + mLastLocation.getLatitude() + "," + mLastLocation.getLatitude());
latitude = mLastLocation.getLatitude();
longitude = mLastLocation.getLongitude();
Log.d(TAG, "latitude = " + String.valueOf(latitude) );
mLatLng = new LatLng(latitude, longitude);
globalLatLng.add(mLatLng);
Log.d(TAG, "Global List = " + globalLatLng);
}
}
}
protected synchronized void buildGoogleApiClient() {
mClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API).build();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
connected = true;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(3000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
mClient.connect();
}
@Override
public void onDestroy() {
Log.i(TAG, "OnDestroy method called");
}
}
My aim is to receive a location save that into a Latlng list and store it into a text file, that can be read and shown on the map in a polyline. I am running them on two seperate threads because googles api clients does not finish so it wont reach any other code.