Do not receive CountDownTimer callbacks in a service

I have a Bound service that collects seats for 3 minutes every 15 minutes. I start CountDownTimer when the Service is connected in onServiceConnectedfrom ServiceConnection.
I get all timer callbacks ( onFinish) ( onTick) perfectly, as far as the activity that is visible bindService.
When the device is locked, I do not receive any updates from the timer.

MyLocationService

public class MyLocationService extends Service implements MyTimerListener{

    private IBinder mBinder = new MyLocationBinder(); 

    public void onCreate() {
        locationManager = new MyLocationManager(this);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return mBinder;
    }

    public class MyLocationBinder extends Binder
    {
        public MyLocationService getService()
        {
            return MyLocationService.this;
        }
    }

    public void startFetchingLocations()
    {
        if(locationManager != null){
            locationManager.startFetchingLocations();
            if(publishPeriodCountDownTimer != null) {
                publishPeriodCountDownTimer.cancel();
                publishPeriodCountDownTimer = null;
            }
            publishPeriodCountDownTimer = new MyCountTimer(gpsPublishPeriod * 60 * 1000, 1000, MyLocationService.this);
            publishPeriodCountDownTimer.start();    
        }
    }


    @Override
    public void onTimeFinished() {
        //Start fetching locations
        locationManager.startFetchingLocations(); //Start 3 min CountdownTimer
    }


    @Override
    public void onTick() {

    }

    public void onAllLocationsRecieved(ArrayList<Location> locations)
    {
        //Do stuff on Locations

        locationManager.stopFetchingGPS(); //Stops 3 min countdownTimer
    }
}

Myactivty

public class MyActivity
    {

            @Override
            protected void onStart() {
                super.onStart();
                btnStop.setVisibility(View.VISIBLE);
                MyNoificationManager.cancelAllNotifications();

                if (!isLocationServiceBound) {
                    Intent locServiceIntent = new Intent(this, MyLocationService.class);    
                    bindService(locServiceIntent, locationServiceConnection, Context.BIND_AUTO_CREATE);
                }
        }

        private ServiceConnection locationServiceConnection = new ServiceConnection(){

            @Override
            public void onServiceDisconnected(ComponentName name) {
                isLocationServiceBound = false;

            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MyLocationBinder locationBinder = (MyLocationBinder) service;
                locationService = locationBinder.getService();
                isLocationServiceBound = true;
                locationService.startFetchingLocations();
            }
        };
    }

Works as expected when activity is visible. The timer does not provide any callbacks onTick()or onTimeFinished()when the device is locked.
What could be the problem here?

+4
1

CountDownTimer , . .

android.app.AlarmManager , . , . ,

   AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
   Intent intent = new Intent(this, MyLocationService.class);
   intent.putExtra("need_to_fetch_loc", true);
   PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
   alarmManager.set(AlarmManager.RTC_WAKEUP, gpsPublishPeriod * 60 * 1000, alarmIntent);

MyLocationService

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {

     if(locationManager!= null && intent.getBooleanExtra("need_to_fetch_loc", false))
     {
         locationManager.startFetchingLocations();
     }

     return super.onStartCommand(intent, flags, startId);
 }
0

All Articles