bound_service 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 48

bound_service #

Bound Service In Flutter.

Getting Started #

This is a simple implementation of a bound service in Flutter. This is based on Android documentation (Bound Services).

Bound Services let you run background code inside your flutter app, even the application is left or open as it uses binding and unbinding of a service.

The simplest way to explain it:

  1. You run flutter application with Flutter Platform Channel For Android
  2. This implementation let your code in Java run in background. Ex. Music, downloads, recording, background location.
  3. When the user leaves your application, the background code will remain opened until it is explicitly closed (by the application or OS).
  4. Since it's a bound service, the service should be bound to an application (obligatory).
  5. If the application is killed, the service is killed also by the system.

Files to check the codes:

  • bound_service.dart -- in charge of communicating with Java
  • main.dart -- Top level communication with Java
  • BoundServicePlugin.java -- It is the plugin where all your background code which runs
  • LocalService.java -- contains all your Local Services in form of public methods, of whatever tricks you would like !

1.0.0 #

  • TODO: Describe initial release.
  • bound service added and run the all widget to background

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:bound_service/bound_service.dart';

void main() => runZoned<Future<Null>>(() async {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
    runApp(MyApp());
  });
}, onError: (error, stackTrace) async {
  debugPrint(error.toString());
},
);

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    // Platform messages may fail, so we use a try/catch PlatformException.
    Timer.periodic(Duration(seconds: 5), _runthis);
  }


  void _runthis(_) async{
    String platformVersion;
    try {
      platformVersion = await BoundService.platformVersion;
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }
    print("RUNNING FROM DART");

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('Running on: $_platformVersion\n'),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  bound_service: ^1.0.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:bound_service/bound_service.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
6
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
48
Learn more about scoring.

We analyzed this package on May 24, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Health issues and suggestions

Document public APIs. (-1 points)

4 out of 4 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/bound_service.dart.

Run flutter format to format lib/bound_service.dart.

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
uri ^0.11.3+1 0.11.3+1
Transitive dependencies
collection 1.14.12
matcher 0.12.6
meta 1.1.8
path 1.7.0
quiver 2.1.3
sky_engine 0.0.99
stack_trace 1.9.3
typed_data 1.1.6
utf 0.9.0+5
vector_math 2.0.8
Dev dependencies
flutter_test