foreground_service 1.1.0+2 copy "foreground_service: ^1.1.0+2" to clipboard
foreground_service: ^1.1.0+2 copied to clipboard

outdated

Flutter plugin for running dart stuff via an Android foreground service. Only barely tested on Android O, and ever so slightly on M. iOS support not planned.

example/lib/main.dart

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

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

  startFGS();
}

//use an async method so we can await
void startFGS() async {
  await ForegroundService.setServiceIntervalSeconds(5);

  //necessity of editMode is dubious (see function comments)
  await ForegroundService.notification.startEditMode();

  await ForegroundService.notification
      .setTitle("Example Title: ${DateTime.now()}");
  await ForegroundService.notification
      .setText("Example Text: ${DateTime.now()}");

  await ForegroundService.notification.finishEditMode();

  await ForegroundService.startForegroundService(foregroundServiceFunction);
  await ForegroundService.getWakeLock();
}

void foregroundServiceFunction() {
  debugPrint("The current time is: ${DateTime.now()}");
}

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

class _MyAppState extends State<MyApp> {
  String _appMessage = "";

  @override
  void initState() {
    super.initState();
  }

  void _toggleForegroundServiceOnOff() async {
    final fgsIsRunning = await ForegroundService.foregroundServiceIsStarted();
    String appMessage;

    if (fgsIsRunning) {
      await ForegroundService.stopForegroundService();
      appMessage = "Stopped foreground service.";
    } else {
      startFGS();
      appMessage = "Started foreground service.";
    }

    setState(() {
      _appMessage = appMessage;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
            child: Column(
          children: <Widget>[
            Text('Foreground Service Example',
                style: TextStyle(fontWeight: FontWeight.bold)),
            Padding(padding: EdgeInsets.all(8.0)),
            Text(_appMessage, style: TextStyle(fontStyle: FontStyle.italic))
          ],
          mainAxisAlignment: MainAxisAlignment.center,
        )),
        floatingActionButton: Column(
          children: <Widget>[
            FloatingActionButton(
              child: Text("F"),
              onPressed: _toggleForegroundServiceOnOff,
              tooltip: "Toggle Foreground Service On/Off",
            )
          ],
          mainAxisAlignment: MainAxisAlignment.end,
        ),
      ),
    );
  }
}
51
likes
0
pub points
67%
popularity

Publisher

unverified uploader

Flutter plugin for running dart stuff via an Android foreground service. Only barely tested on Android O, and ever so slightly on M. iOS support not planned.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on foreground_service