4

I have built an android app that performs some downloads followed by some time consuming processing. My goal here is to do the downloading and the processing in the background. I have read about background and foreground services, but I am not able to understand them properly and which to use where.

I have built the rest of the app with ionic. Now I have to make the app work in background. I have tried cordova-plugin-background-mode available in ionic but unfortunately its not maintained anymore.

So what should I do to my app in android studio to make it support background processing. Also is it possible to combine android packages to an ionic project after building it?

Thanks in advance.

  • Does this answer your question? [Cordova scheduling task](https://stackoverflow.com/questions/31517871/cordova-scheduling-task) – Muhannad Fakhouri Dec 13 '21 at 06:45
  • @MuhannadFakhouri No I have tried that and the app stopped after some time. In case of my android studio SDK which was android 9 it ran for about 2500 seconds. Also in case of android 10/11 in mobile device, it ran for about 1500s – Sudharshan R Dec 13 '21 at 10:00
  • Did you have a look at this too https://ionicframework.com/docs/native/foreground-service ? – Muhannad Fakhouri Dec 13 '21 at 10:25
  • Just after you suggested, I tried it out. It seems to do the trick. but sometimes it just closes on its own. I just did a simple counter that increments every second by 1. sometimes it ran for hours and at other times it closed in leas than 20 minutes – Sudharshan R Dec 14 '21 at 04:50
  • You can't rely on a Service existing for a long period of time on Android. They can be killed at any time by the OS. Background services will be killed in 2 minutes after your app is no longer foreground. Foreground services will last longer, but not indefinitely. Your best bet is to use WorkManager to do the processing. – Gabe Sechan Sep 05 '22 at 04:18

1 Answers1

4

First understand about Android services: Three different types of services:

1. Foreground service: is a service that stays alive even when the app is terminated. Foreground services continue running even when the user isn't interacting with the app.

List of Apps:

  • Music player app that plays music in a foreground service
  • Fitness app that records a user's run in a foreground service
  • Navigation app, allows users to get turn-by-turn directions
  • Even you perform your download

Note: To download and the process in the background Google recommend you to use WorkManager.

Let's understand background work:

An app is running in the background when both the following conditions are satisfied:

  • None of the app's activities are currently visible to the user.
  • The app isn't running any foreground services that started while an activity from the app was visible to the user.

2. Background service: is a service that runs only when the app is running so it’ll get terminated when the app is terminated. It performs an operation that isn't directly noticed by the user.

List of Apps:

  • Downlead data from server
  • Continuously share location
  • Sync data with server also use workmanager
  • IOT Apps

3. Bound service: is a service that runs only if the component it is bound to is still active. A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

  • All above apps can be bounded or not
Subarata Talukder
  • 5,407
  • 2
  • 34
  • 50