1

I try to run app in background to detect inclination of device.

To do this I ear that I need to use isolate. I test a code with isolate and add my logic to detect vertical angle. It's work but only 20seconds on my iphone.

How can I run a simple accelerometer continually in background ?

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter_isolate/flutter_isolate.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sensors/sensors.dart';


void isolate2(String arg) {
  getTemporaryDirectory().then((dir) {
    print("isolate2 temporary directory: $dir");
  });
  Timer.periodic(
      Duration(seconds: 1), (timer) => print("Timer Running From Isolate 2"));
}

void isolate1(String arg) async {
  /*final isolate =*/ await FlutterIsolate.spawn(isolate2, "text2");

  getTemporaryDirectory().then((dir) {
    print("isolate1 temporary directory: $dir");
  });
  Timer.periodic(
      Duration(seconds: 1), (timer) => print("Timer Running From Isolate 1"));
}

void main() async {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


  Future<void> _run() async {
    final isolate = await FlutterIsolate.spawn(isolate1, "text1");
    accelerometerEvents.listen((AccelerometerEvent event) {
      print(event.y);

      if (event.y>5.0||event.y<-5.0){
        print("vertical detection");

      }
    });

  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: ElevatedButton(
            child: Text('Run'),
            onPressed: _run,
          ),
        ),
      ),
    );
  }
}

thank you

Nitneuq
  • 3,866
  • 13
  • 41
  • 64

1 Answers1

1

Use workmanager this helps us to perform background job

dependencies:
  workmanager: ^0.2.3

Two ways to perform background job

1. Delayed background work

registerOneOffTask runs the task only once with an initial delay of 10 seconds. This is useful when we need to perform any background work only once.

Example:

Workmanager.registerOneOffTask(
  "1",
  "registerOneOffTask",
  initialDelay: Duration(seconds: 10),
);

2. Periodic background work

This task runs periodically, Since we have not provided a frequency it will be the default 15 minutes. Example:

Workmanager.registerPeriodicTask(
  "2",
  "registerPeriodicTask",
  initialDelay: Duration(seconds: 10),
);
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • Workmanager last update was in July 2020, and it support mostly Android. Maybe he can try https://pub.dev/packages/background_fetch – BLKKKBVSIK Mar 19 '21 at 15:46
  • I used it, it's working fine, for him, it should work for him as is a simple requirement. – Jitesh Mohite Mar 19 '21 at 15:52
  • Thank you, I tried but I have same comportment that isolate. consol display print only 20s after background. Perhaps it's work but I can monitor/verify I try to add vibrator() to test if my detection run in background but it work only when I'm not in background – Nitneuq Mar 21 '21 at 07:53
  • Which mobile phone you are using? – Jitesh Mohite Mar 21 '21 at 08:30
  • sorry I need iOS and android, just see iOS isn't supported – Nitneuq Mar 21 '21 at 08:44