I am trying to call the method continuously but when I lock the screen of my phone or press the home button the method not get called again. so plz suggest how to run the method in the background.
2 Answers
You'll have to use a plugin such as https://pub.dev/packages/background_fetch
There are other similar plugins you may try.
"Background Fetch is a very simple plugin which 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."

- 5,673
- 3
- 16
- 29
-
i had tried this plugin. but i get this error after installing this plugin. – Karan Jain Oct 28 '20 at 13:06
-
4What went wrong: Could not determine the dependencies of task ':app:processDebugResources'. > Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'. > Could not find any matches for com.transistorsoft:tsbackgroundfetch:+ as no versions of com.transistorsoft:tsbackgroundfetch are available. Searched in the following locations: - https://dl.google.com/dl/android/maven2/com/transistorsoft/tsbackgroundfetch/maven-metadata.xml - https://dl.google.com/dl/android/maven2/com/transistorsoft/tsbackgroundfetch/ – Karan Jain Oct 28 '20 at 13:07
-
the above was my error i got while running my app – Karan Jain Oct 28 '20 at 13:08
-
I have same issue – Faiz Anwar Jan 04 '21 at 07:11
-
@KaranJain You have to follow the setup instructions on the GitHub page very carefully. I had this issue even after thinking I'd followed the instructions but it turned out I had modified the wrong repository section of build.gradle. – Barry Feb 25 '22 at 20:38
You will use android alarm manager for android apps using Flutter
https://pub.dev/packages/android_alarm_manager
import 'package:android_alarm_manager/android_alarm_manager.dart';
void printHello() {
final DateTime now = DateTime.now();
final int isolateId = Isolate.current.hashCode;
print("[$now] Hello, world! isolate=${isolateId} function='$printHello'");
}
main() async {
final int helloAlarmID = 0;
await AndroidAlarmManager.initialize();
runApp(...);
await AndroidAlarmManager.periodic(const Duration(minutes: 1), helloAlarmID, printHello);
}
As you can see, is simple, you only need to create a function and call it using AndroidAlarmManager.periodic, remember to add permissions on your android manifest.
If you need a solution for Android and iOS you can use platform channels https://flutter.dev/docs/development/platform-integration/platform-channels you can use platform channels to call native code from android an iOS, for example you can create native background services and call it from Flutter.

- 11
- 2