Execute Dart functions in the background when flutter application autostarts at BOOT_COMPLETED?
In my Flutter app, I want to execute a small Dart function in the background as soon as the app receives the Autostart on BOOT_COMPLETED message, rather than loading the entire app onto the screen.
When the user launches the app from the launcher, the app should
load fine, but when the app receives the Autostart on BOOT_COMPLETED message, it should just run a small dart function in the background without the app actually loading onto the screen.
So far, I’ve used the following git >> code
But this code runs the entire app on the screen.
I’m not an Android developer, so I can’t be sure what’s really going on in the list file.
So in the example below, I want to execute backgroundfunction() only when the app receives an Autostart on BOOT_COMPLETED message
import 'package:flutter/material.dart';
void main() {
runApp(new MyApp());
}
void backgroundfunction(){
print("Hello world");
}
Solution
You can use background_fetch plugin and make sure:
- You are interested in headless mode documentation
- By Enable startOnBoot: true during configuration of the service
use a BOOT_COMPLETED receiver on Android < 21 or on Android >= 21