1

I have a class for using FusedLocationApi to send the location of user frequently to the server. All methods for getting location updates and communicating with server are set in this class except that when I want to run this class, I call a background service and then use this class inside it.

Uisng FusedLocationApi.requestLocationUpdates , the location is being sent to the server properly and I can see Toasts from this service. But when I check the Running services on android system, my application is not listed there.

My question is that why my service is not running anymore but the location is being updated? Is the FusedLocation Service is a background service itself? Do I need to call it frequently through a background service to keep it alive or I can call it inside the activity and let it continue running even after closing the app?

this is how I use the class inside my own service:

public class LocationService extends Service {

    FusedClass mylocation=new FusedClass(this);

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent.getAction().equals("turnOn")) {
            HandlerThread handlerThread = new HandlerThread("LocationThread");
            handlerThread.start();

            Handler handler = new Handler(handlerThread.getLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    mylocation.LocationStartPoint();
                }
            });
        }
        if (intent.getAction().equals("turnOff")) {
            stopSelf();
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        //TODO for communication return IBinder implementation
        return null;
    }

EDIT: I use FusedLocationApi.requestLocationUpdates and onLocationChanged to get location updates from this API.

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • 1
    FusedLocationApi work on background. in My case it works via Firebase JobService. Every 20 minuts i run job (with notification android 8 need it) that check current location and save it to firebase. – Vadim Eksler Aug 09 '18 at 08:26
  • Why do you call it every 20 minutes? Doesn't `requestLocationUpdates` do that frequently? @VadimEksler – Ali Sheikhpour Aug 09 '18 at 08:34
  • I worried about user battery. I use just getLastLocation and not requestLocationUpdates – Vadim Eksler Aug 09 '18 at 08:36
  • In case that user has google maps)) and location service turned on google make requestLocationUpdates and give it to me by getLastLocation – Vadim Eksler Aug 09 '18 at 08:44

0 Answers0