I have a location listener in an Android widget that is declared in class MLocation as,
private static MyLocationListener locationListener;
The widget starts a service which in turn creates the MLocation class. The MLocation class creates and registers the location listener on creation.
Everything works fine until the phone sleeps. When the phone sleeps (some time after it sleeps), Android kills the service and all classes created by it (MLocation too). On phone unlock, Android recreates the service which in turn, creates MLocation class etc. During phone sleep, the location no longer listens for changes (since it is killed). Is there a way to keep the location listener alive, even if the phone sleeps?
No. When the phone sleeps, no basically shuts down the CPU so that no applications are running. If you need to listen for location changes while the phone is sleeping, you’ll need to prevent it from sleeping by obtaining a wakelock. Be warned that if you do that you will use up the battery pretty quickly and your users will uninstall your application.