Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best architecture for a long running service on Android

I would appreciate some guidance on how to deal with OS killing a long run service.

Business scenario:

Application records a BTT track which may last for several hours. It can also show the track on map together with relevant statistics.

The application user interface enables the user to start/stop track recording and view the real time track on a map.

After start track recording user can exit the application and turn screen off (to save power), and only a service will remain running to keep the recording update to database (notification shown), until the user starts again the activity and ask for stop recording, which results in service termination.

Issue:

After a variable time, which runs from 40 minutes to 1 hour and a half, the recording service gets killed without any warning. As BTT outings may take several hours, this result in track recording incomplete.

Some additional information:

Service is started with START_STICKY and acquires a PARTIAL_WAKE_LOCK, and runs in the same process as the main activity.

New locations are acquired (and recorded) at user defined rate from 1 second to several minutes. I know from the Android documentation that this is the expected OS behavior for long running services.

Question:

What is the best architecture design approach to have a well behaved application that could satisfy the business scenario requirements?

I can think of a couple of options (and I don’t like any of them), but I would like guidance from someone how have already faced and solved similar issue:

  • Use broadcast receiver (ideally connected to Location Manager if that’s possible) to have the service only running when a new location is acquired?
  • Do not enable the user to leave the main activity (resulting in pour user experience)?
  • Have an alarm broadcast receiver restarting the service if needed?

Thanks to all who could share some wisdom on this subject.

like image 470
Luis Avatar asked Oct 02 '12 14:10

Luis


People also ask

Which architecture is best for Android?

The most popular android architectures used by developers are the following: MVC (Model — View — Controller) MVP (Model — View — Presenter) MVVM (Model — View — ViewModel)

Which layer of Android high level architecture has Android system services?

Hardware abstraction layer (HAL).

Which architecture is used for Android devices?

The foundation of the Android platform is the Linux kernel. For example, the Android Runtime (ART) relies on the Linux kernel for underlying functionalities such as threading and low-level memory management.


1 Answers

I have an app that does a very similar thing. I make sure the service keeps running by making it a foreground task. When I am ready to start running, I call this function, which also sets up a notification:

void fg() {
    Notification notification = new Notification(R.drawable.logstatus, 
                    "Logging On", System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, LoggerActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
                    notificationIntent, 0);
    notification.setLatestEventInfo(this, "Logger","Logger Running",
                pendingIntent);
    startForeground(1, notification);   
}

and then to leave foreground mode when logging is finished:

stopForeground(true);
like image 92
Michael Avatar answered Nov 15 '22 18:11

Michael