Skip to content

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

package com.example.flutter_app_services;

import android.os.Bundle;


public class MainActivity extends FlutterActivity {

    public void onCreate(Bundle savedInstanceState) {



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

import android.content.*;
import android.os.*;
import android.widget.Toast;

public class ServiceBack extends Service {

    public Context context = this;
    public Handler handler = null;
    public static Runnable runnable = null;

    public IBinder onBind(Intent intent) {
        return null;

    public void onCreate() {
        Toast.makeText(this, "Start", Toast.LENGTH_LONG).show();

        handler = new Handler();
        runnable = new Runnable() {
            public void run() {
                Toast.makeText(context, "In Run....", Toast.LENGTH_LONG).show();
                handler.postDelayed(runnable, 10000);

        handler.postDelayed(runnable, 15000);

    public void onDestroy() {
        Toast.makeText(this, "Stop", Toast.LENGTH_LONG).show();

    public void onStart(Intent intent, int startid) {
        Toast.makeText(this, "initialized", Toast.LENGTH_LONG).show();

Start Service

startService(new Intent(this,ServiceBack.class));

This is my AndroidManifest.xml

<manifest xmlns:android=""

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />


       <service android:enabled="true" android:name=".ServiceBack" />
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>

        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate -->
            android:value="2" />

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.


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.