Reputation: 535
I'm developing an android application to track user location in background and keep updating it to firebase database as location changes the app is working fine when the application is opened but when i close it , firebase stop setting new values , it will set the new values directly after I open the application again
this is my android service code :
public class LocationServiceFB extends Service implements LocationListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
private Location mLastLocation;
DatabaseReference instructors;
GeoFire geofire;
String UserId;
SharedPreferences pref;
GoogleApiClient mGoogleApiClient;
LocationRequest mLocationRequest;
private static int UPDATE_INTERVAL = 5000;
private static int DISPLACEMENT = 10;
private static int FATEST_INTERVAL = 3000;
private LocationManager mLocationManager;
@Nullable
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not Yet Implemented");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
instructors = FirebaseDatabase.getInstance().getReference("OnlineInstructors");
geofire = new GeoFire(instructors);
pref = this.getSharedPreferences("LoginTrack", Context.MODE_PRIVATE);
UserId = pref.getString("firebasekey","");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
instructors.child(UserId).removeValue();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FATEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this);
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
geofire.setLocation(UserId, new GeoLocation(location.getLatitude(), location.getLongitude()), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
Log.d("ERROR" , "INSTRUCTOR LOCATION SENT TO DATABASE" );
}
});
}
public void uploadLocation(){
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d("EER","np permissions");
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
final double latitude = mLastLocation.getLatitude();
final double longitude = mLastLocation.getLongitude();
geofire.setLocation(UserId, new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() {
@Override
public void onComplete(String key, DatabaseError error) {
Log.d("ERROR" , "INSTRUCTOR LOCATION SENT TO DATABASE" );
}
});
}
}
Upvotes: 1
Views: 704
Reputation: 66
You are using a bound service for upadating your position to the server. Your service gets killed along with app. Use an unbound service. Unbound service keeps running in the background even when the app is killed. So, this will work fine even in the background with unbound service.
Upvotes: 0
Reputation: 317758
Android may kill your app's process in favor of more important apps. Android may also stop your process from networking when it's not longer visible in the foreground. This is to prevent poorly behaved apps from consuming too many resources.
If your apps needs to continue networking when the user is no longer using it, you'll have to start a foreground service, which also requires that you show a notification that indicates to the user that your app is still running.
Upvotes: 1