1

I'm developping an app for a company who wish to use android phones as persistent user location info. I've created a LocationListener, and requestLocationUpdates, generating a cached process, which saves the data to a file. BUT, it's usual for the system to kill those processes to make up space for other proceses, and I want to negate that.

In words, I want for those cache processes to me persistent, and resist to kill commands (or stop the kill commands from the system). Already tried activities to no avail (kept crashing the app)

Right now the system is somethin like this:

LocationListener listener = new LocationListener(){
    onLocationChanged(){useData;/*congestion avoidance function*/}    

    onStatusChanged()
    {
    if(offline){requestLocationUpdate(/*with new time, to avoid congestion*/)}

    }
}

It's a very rough draft, hope you can get along with it. inbetween there are some functions to avoid battery drain and memory congestion

The process works fine, and it is persistent, sometimes. But some other times phone usage kills that process, and I want to avoid that.

I'm relatively new to android programming, but have experience in other systems.

Thanks in advance

fusion--
  • 43
  • 1
  • 7
  • You can not stop the killing. If Android decides that it needs resources that you occupy it's going to kill you. You can increase your [importance](http://developer.android.com/guide/components/processes-and-threads.html#Lifecycle) by being a foreground service, request that you get re-started in case you were killed (`START_STICKY`) and add code that regularly tries to restart your service (e.g. `AlarmManager`). There are [several](http://stackoverflow.com/q/17005837) [answers](http://stackoverflow.com/q/4708827) that already explain how it works. – zapl Dec 03 '13 at 18:14

1 Answers1

0

You should put your LocationListener in a Foreground Service. The Foreground Service is the only service type that will not be stopped by the system when memory gets low (it also has to have an icon in the system bar). You can read more on the implementation of them in the Android Documentation

RocketSpock
  • 2,031
  • 16
  • 11
  • Technically, it's more that a foreground service is comparatively unlikely to be stopped by the system when memory gets low. – CommonsWare Dec 03 '13 at 18:30
  • @CommonsWare Verbatim from the Android Docs "A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory". I agree that it is comparatively unlikely to be stopped, however it is just as likely that the system UI will be stopped. – RocketSpock Dec 03 '13 at 19:44
  • I'm gonna try that now. Come back with results later. – fusion-- Dec 04 '13 at 09:55