Skip to content
Advertisement

Background service on Android with java using flutter project

I am new to flutter, I am trying to create a service in the background when starting Android occupying the main java class since in flutter it cannot be done, so I want to start a service in its background from here

JavaScript

I have seen many queries with the following code: Java class

JavaScript

Start Service

JavaScript

This is my AndroidManifest.xml

JavaScript

The main problem is that I don’t know how to implement it in the “Flutter Activity”. I am trying to make a service that starts in BOOT and when I close the main application the service continues in the background (it is important that it does not stop), but none of this works either, I need help, I have looked everywhere and nothing. I use the Android 11 operating system.

Advertisement

Answer

You won’t get what you want. In Android, there are 2 types of services- Foreground and Background.

Background services can be killed at any time. They will be killed by the OS a few minutes after starting. They’re meant for short duration tasks in the background that need a valid Android Context object.

Foreground services can be killed at any time. They show a notification to the user that the service is running, so the user knows what’s going on at all times. Foreground services are lower on the kill list than background services so they won’t be killed immediately, but the OS can and will kill them for resources whenever it thinks it needs them. They are not reliable.

There is no reliable way to run a background service like you would on a PC. Instead, you need to architect your code to work on an event driven basis so that it can respond to OS or time events and do work as needed, rather than waiting around for a request to be made.

More focused advice on how to do that would require a lot more details about what you’re trying to do.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement