Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continually Running Background Service

Tags:

I'm targeting sdk version 27 with a minimum version of 19 and trying to get a service that runs continuously in the background. I tried different service start options but it still got killed with the app. I tried using a BroadcastReceiver to start the service when it got killed but that gave me an error saying that the app was in the background and couldn't start a service so I tried using the JobScheduler and that gave me the same error. How is this supposed to be done? For example, if I were making a pedometer app, how could I keep that running in the background?

like image 282
CaseyB Avatar asked Jul 11 '18 15:07

CaseyB


People also ask

How do I run background services continuously?

This example demonstrates how do I run an android service always in background. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What are background services in Android?

A background service performs an operation that isn't directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service.

Why is the background intelligent transfer service not running?

It is normal behaviour for the The Background Intelligent Transfer service to be stopped. I have just checked my services and it is not running and set to manual. it is triggered and started by the update service automatically. Please follow the steps below to reset Windows Update components.

How to start the foreground service from the background service?

A working hack for this is to simply start a foreground service which is only visible for the fraction of a second and starts your background service. In the background service you'd then periodically start the foreground service.

How do I use scoped services within a backgroundservice?

To use scoped services within a BackgroundService, create a scope. No scope is created for a hosted service by default. The scoped background task service contains the background task's logic. In the following example:

How to disable all non-Microsoft services to fix too many background processes?

You have a quick way to disable all non-Microsoft services to fix too many background processes Windows 10, namely, to use Windows System Configuration tool. Step 1. Press Windows + R on the keyboard, type msconfig, and hit Enter to open System Configuration window.


3 Answers

In oreo release Android defined limits to background services.

To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.

Still if app need to run its service always, then we can create foreground service.

Background Service Limitations: While an app is idle, there are limits to its use of background services. This does not apply to foreground services, which are more noticeable to the user.

So create a foreground service. In which you will put a notification for user while your service is running. See this answer (There are many others)

Now what if you don't want a notification for your service. A solution is for that.

You can create some periodic task that will start your service, service will do its work and stops itself. By this your app will not be considered battery draining.

You can create periodic task with Alarm Manager, Job Scheduler, Evernote-Jobs or Work Manager.

  • Instead of telling pros & cons of each one. I just tell you best. Work manager is best solution for periodic tasks. Which was introduced with Android Architecture Component.
  • Unlike Job-Scheduler(only >21 API) it will work for all versions.
  • Also it starts work after a Doze-Standby mode.
  • Make a Android Boot Receiver for scheduling service after device boot.

I created forever running service with Work-Manager, that is working perfectly.

like image 68
Khemraj Sharma Avatar answered Sep 30 '22 09:09

Khemraj Sharma


Since Android 8.0 many background service limitations have been introduced.

Two solutions:

  1. if you need to get total control of task and execution timing, you have to choose Foreground Service. Pros: your app will be considered to be alive, then is more unlikely that the os will kill it to free resources. Cons: your user will always see the Foreground Notification.

  2. if you need to schedule periodically task, then Work Manager (introduced in Google I/O 18) is the best solution. This component choose the best possible scheduler (Jobscheduler, JobDispatcher, AlarmManager..). Keep in mind that work manager APIs are useful only for the tasks that require guaranteed execution and they are deferrable. Ref: Android Dev Documentation

like image 33
Andrea Scalabrini Avatar answered Sep 30 '22 09:09

Andrea Scalabrini


The only solution I would suggest is using Firebase Cloud Messages. Or foreground services.

like image 41
Vyacheslav Avatar answered Sep 30 '22 09:09

Vyacheslav