US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No 405, Kabir Shilp, Opp. Kansar Hotel, Opp. Landmark, Kudasan, Gandhinagar, Gujarat 382421

[email protected]

How to Create a Service that Makes App Always Runs In a Background ?

How to Create a Service that makes App Always Runs In a Background

How to Create a Service that Makes App Always Runs In a Background ?

While Developing a Flutter Mobile Application it may happen that we need to perform a separate operation for Foreground and Background. So in this article, we will discuss How to Create a Service that makes App Always Runs In a Background?

How to Create a Service that makes App Always Runs In a Background?

There isn’t a way to do this directly from flutter right now although that may change at some point – see this bug/feature request. You do have a couple of options though.

The first is to use MethodChannels and simply write the android code you want to create a background service (or if you want it to always be a background service you can probably do that without needing communication from the flutter side).

The second is some combination of these two plugins – android_alarm_manager and android_intent. But that won’t help with all use-cases.

Create a New Flutter project.

  • Create BroadcastReceiver class beside MainActivity in android dir.
  • Change and main.dart and AndroidManifest.xml like this


package com.example.flutter_broadcastreceiver_alarmmanager_repeat;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  public class MyReceiver extends BroadcastReceiver {
      public void onReceive(Context context, Intent intent) {

MainActivity will have a code snippet like below:

package com.example.flutter_broadcastreceiver_alarmmanager_repeat;

import android.content.Intent;
import android.os.Bundle;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.view.FlutterView;

public class MainActivity extends FlutterActivity {

    private PendingIntent pendingIntent;
    private AlarmManager alarmManager;
    private static  FlutterView flutterView;
    private static final String CHANNEL = "com.tarazgroup";

    protected void onCreate(Bundle savedInstanceState) {


        Intent intent = new Intent(this, MyReceiver.class);
        pendingIntent = PendingIntent.getBroadcast(this, 1019662, intent, 0);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000, pendingIntent);


    protected void onDestroy() {

    static void callFlutter(){
        MethodChannel methodChannel=new MethodChannel(flutterView, CHANNEL);
        methodChannel.invokeMethod("I say hello every minute!!","");

Our Main.dart will have a code snippet like the below:

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
      home: MyHomePage(),

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  _MyHomePageState createState() => _MyHomePageState();

class _MyHomePageState extends State<MyHomePage> {

  static const methodChannel = const MethodChannel('com.tarazgroup');

  _MyHomePageState() {
    methodChannel.setMethodCallHandler((call) {

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
      body: Container() 

We also need to make changes in AndroidManifest.xml like the below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""


                android:value="true" />

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

        <receiver android:name=".MyReceiver"></receiver>



There is also a plugin called background_fetch

Background Fetch is a very simple plugin that will awaken an app in the background about every 15 minutes, providing a short period of background running-time. This plugin will execute your provided callbackFn whenever a background-fetch event occurs.


In this article, We have been through How to create a service that makes the app always runs in a background?

Keep Learning !!! Keep Fluttering !!!

Still, need a Support for Flutter Development? Do let us know. is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc. is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Post a Comment