Flutter-background process - flutter

I have a question about background process in flutter. The app I am building needs to constantly work in the background and use live accelerometer data. I have been over many libraries and tutorials, but I just can find something which will work constantly in the background. I used the flutter_background_servicepackage and there is a notification on the top of the screen which says "background service- preparing." Any suggestions?
void onStart() {
WidgetsFlutterBinding.ensureInitialized();
final service = FlutterBackgroundService();
service.setForegroundMode(false);
}
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.inactive ||
state == AppLifecycleState.detached) return;
final isBackground = state == AppLifecycleState.paused;
if (isBackground) {
number=1;
}
else{
number=0;

Related

How to check if App is not being used while in Foreground in Flutter?

In my flutter app, I wanted to check if a user is not using the app while they are in Foreground. WidgetsBindingObserver only checks if the app is in the foreground/background. and if the app has resumed, inactive, detached, or paused. but how can I check if the user is not using the app in the foreground at a specific time?
#override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
#override
void dispose() {
// TODO: implement dispose
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (state == AppLifecycleState.detached ||
state == AppLifecycleState.inactive) return;
final isBackground = state == AppLifecycleState.paused;
final isForeground = state == AppLifecycleState.resumed;
if (isBackground || isForeground) {
Get.offAll(Login());
}
}
You need to run a periodic timer that gets reset whenever the user taps the screen.
So set up something global to do just that. Set it to X seconds and reset to 0 when the user tap is detected. If the timer reaches Y seconds you know that it is not used by the user.
Flutter - Detect Tap on the Screen that is filled with other Widgets
https://blog.logrocket.com/understanding-flutter-timer-class-timer-periodic/
Hopes this helps you in the right direction

Flutter audio_service media resume

I have custom queuing logic using MediaItem which works properly when the UI is active. I am stopping the audio playback using onTaskRemoved method.
#override
Future<void> onTaskRemoved() async {
//if (!AudioServiceBackground.state.playing) {
logger.v("Task Removed");
await stop();
//}
}
#override
Future<void> stop() async {
await _player.stop();
await playbackState.firstWhere(
(state) => state.processingState == AudioProcessingState.idle);
}
However after swiping the task, notification is still visible and when I click the notification to resume nothing happens and app is stuck in splash screen.

apply didChangeAppLifecycleState to the whole app

Hey I have this simple code for didChangeAppLifecycleState that triggers a function that updates a Firestore record of when user was online last time
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
final authServiceProvider =
Provider.of<AuthServiceProvider>(context, listen: false);
if (state == AppLifecycleState.resumed) {
} else {
authServiceProvider.updateUserLastOnline();
}
}
It works fine in a separate page but how I make it work for the whole app not copying this code to every single page?
I just converted my main widget in main.dart into Stateful widget with WidgetsBindingObserver and added the same code
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
final authServiceProvider =
Provider.of<AuthServiceProvider>(context, listen: false);
if (state == AppLifecycleState.resumed) {
} else {
authServiceProvider.updateUserLastOnline();
}
}
Now it works for the whole app.

How to execute some code just before app is swiped off from task list in flutter?

I want to execute some code(say calling a REST API) just before the App is swiped off the task list. So far I've tried used WidgetsBindingObserver to detect when the app reaches detached state. The connection is lost before reaching the detached state.
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
if (_lastLifecycleState == AppLifecycleState.detached) {
print('DETACHED!');
}
}

Flutter : AppLifecycleState not work if devices in locked screen

I use AppLifeycleState to detect Behaviour my app when the user using it. For detect my app not being used, i used
AppLifecycleState.paused
AppLifecycleState.inActive
AppLifecycleState.detached
Because i want detect if my App not being used for 1 second, i throw user to Confirmation Fingerprint (Like Whatsapp). Everything is fine i can detect if my app not being used if user press recent app, Home button and throw user to Confirmation Fingerprint if user comeback again.
But the problem is , When devices screen in locked up then it opened again , user not throwing to Confirmation Fingerprint.
I surely can see in console my app is AppLifecycleState.paused and AppLifecycleState.inActive, but strangely if I hot reload ctrl +s in my IDE my app throwing me to Confirmation Fingerprint.
Why can be like this ?
Working 1
Working 2
Not Working !
Already tested too in real Device (Redmi Note 4) , and still not working.
Source Code AppLifecycleState
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
setState(() => _appLifecycleState = state);
if (widget.userBox == null) {
print('Box User Null');
return;
} else {
if (widget.userModelHive.fingerPrint || widget.userModelHive.pinCode) {
if (_appLifecycleState != AppLifecycleState.resumed) {
//? Berarti Lifecylenya InActive , Paused , Detached , Maka Simpan Waktu Keluarnya
final actionBox = await repository.changeDateExit(
userModelHive: UserModelHive()
..id = widget.userModelHive.id
..giverName = widget.userModelHive.giverName
..pinCode = widget.userModelHive.pinCode
..fingerPrint = widget.userModelHive.fingerPrint
..darkMode = widget.userModelHive.darkMode
..tokenExpiry = true
..durationToken = widget.userModelHive.durationToken
..dateExit = DateTime.now()
.add(Duration(seconds: widget.userModelHive.durationToken)),
);
print(state);
return actionBox;
} else {
print('Resumed Lifecycle');
}
}
}
if (mounted) {
setState(() {});
}
super.didChangeAppLifecycleState(state);
}