androidservicelocationlistener

Android back ground service with location listener


So I have a background service contains location listener, what I expect is when I press the home button, the location listener should be invoked since the location is changed, and print a message in the logs, so I know it is still listening to the location change, but what happen is:

-if I am in my application activity, then I pressed the home button, and then I changed the location, I see a message in the logs confirming that the listener noticed location change, but when I change the location again, it is like the listener in pause state, so any attempt of changing the location doesn't invoke the listener and I don't see any message in logs, this happen after the first invoke, the second, third, etc..., location listener doesn't do anything.

Returning to application main activity, it will actually be invoked again and all the time with no issues, unless I pressed the home button again.

here is my service class

package com.example.husseinjehadalhroub.betawaytracker;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import static android.os.Process.THREAD_PRIORITY_BACKGROUND;

public class GpsService extends Service {

    private Looper mServiceLooper;
    private ServiceHandler mServiceHandler;
    private LocationManager locationManager;
    private int counter = 0;





    // Handler that receives messages from the thread
    private final class ServiceHandler extends Handler implements LocationListener {
        public ServiceHandler(Looper looper) {
            super(looper);
        }


        @SuppressLint("MissingPermission")
        @Override
        public void handleMessage(Message msg) {
            initializeLocationManager();
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 3, this);


        }

        @Override
        public void onLocationChanged(Location loc) {
            System.out.println("Counter = " + ++counter);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    }


    @Override
    public void onCreate() {
        // Start up the thread running the service. Note that we create a
        // separate thread because the service normally runs in the process's
        // main thread, which we don't want to block. We also make it
        // background priority so CPU-intensive work doesn't disrupt our UI.

        HandlerThread thread = new HandlerThread("ServiceStartArguments",
                THREAD_PRIORITY_BACKGROUND);
        thread.start();

        // Get the HandlerThread's Looper and use it for our Handler
        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        // For each start request, send a message to start a job and deliver the
        // start ID so we know which request we're stopping when we finish the job
        Message msg = mServiceHandler.obtainMessage();
        msg.arg1 = startId;
        mServiceHandler.sendMessage(msg);

        // If we get killed, after returning from here, restart
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't provide binding, so return null
        return null;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
    }


    private void initializeLocationManager() {
        if (locationManager == null)
            locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }


}

Solution

  • greeble31 Answer: Your app and its service are most likely being destroyed in order to free up resources. See here. You may want to go with a foreground service instead.