I am developing a mobile application that has to monitor several behaviours a user might do. One of the issues i have is low battery performance. I was wondering, is it best to have a modular architecture and several services each with its own purpose, or should i have only one service that captures all the data.
What i am experiencing when i have a lot of services, like 10 or 11 is that i have very low battery performance, although it is very modular.
Which type of programming should i use and why?
I am referring to taking pictures, getting a user location, measuring sound amplitudes, getting accelerometer values, accessing sms and mms content provider.
I also have a service that gets the services data and sends it to a server. here is a sample code:
@Override
public void onCreate() {
Log.e(TAG, "onCreate");
Intent intent = new Intent(this, WorkingService.class);
PendingIntent pintent = PendingIntent.getService(
getApplicationContext(), 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
calendar.setTimeInMillis(System.currentTimeMillis());
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
15000, pintent);
}
this will call the onstartcommand of this class every 15 seconds.
@SuppressWarnings("static-access")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
The number of services should not matter very much to impact battery life. What does matter far more importantly is what those services are doing and how you start them.
For example if you have a service monitoring the user's location, don't start it on a timer that polls the location. Use the geofencing ability in the OS (see here); this is a mistake I see all the time.
In general do not use the alarm manager to run your service(s), this will kill the battery. Use intents, broadcast events, or responses to user interaction with your UI.
You didn't mention what "behaviours a user might do" so it's hard to be more specific than that.