This question is kind of a balancing act between the more philosophical question of how to write good and nice programs and the rather fact based question of how to preserve battery and system resources. Specifically, its about Android-services that, to perform the task needed, have to run permanently in the background on the phone.
In my case I have several services running in the background. One scans if the phone is connected to WiFi and, if so, sets a variable, one checks for the location and updates the location if a precise enough location has been found, one checks the proximity-sensor, one analyses the noise level, on checks an RSS-Feed for updates…
The app has several problems and I think they may stem from this design decision. Mostly, (except with the location-service and the proximity-sensor which offer updates through the SensorEventListener) the architecture of all my services looks like this
public class ArchetypicalService extends Service
{
Checker checker;
public SharedPreferences prefs;
public final static String PREFS = Constants.PREFS;
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
prefs = getSharedPreferences(PREFS, 0);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
checker = new Checker();
checker.run();
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
private class Checker implements Runnable
{
private Handler handler = new Handler();
public static final int checkingDelay = 1000; // this number varies depending on what purpose this service has
@Override
public void run()
{
//what happends here depends on what the service does
handler.postDelayed(this, checkingDelay);
}
}
}
The services are startetd when the app is started. Like this:
Intent intent = new Intent(this,ArchetypicalService.class);
startService(intent);
Thanks to START_STICKY the service will keep running even when we stop the activities. This all seemed text book to me when I wrote it but the more I work with android the more I come across blog-entries that tell me that permanently running services should not be implemented. That they are, above being bad style, make it more likely for the app to crash and eat up the battery and phone-resources. This may all be right: my app crashes a lot and depletes the battery.
So, my question is: If a functionality, like a sensor sweep or checking data on the internet, has to be done regularly in the background, even if all activities have been closed, is there any better way to implement it? (and while I welcome all answers or ideas I would welcome those that provide some helpful example code even more)
http://developer.android.com/guide/components/services.html. Services do notcreate seperate threads. You need to run them in threads if its a long running operation. So if there are lot of services then running everything on the main ui thread will consume memory. Make sure to run in threads. Might solve your app crash problem. Also check this link. How can I keep requesting a page every 5 seconds and not kill the battery?.