background_geolocation_plugin 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 66

background_geolocation_plugin #

Background geolocation plugin for Flutter (Android only).

Features #

  • Android Foreground service make sure to save geolocations when application is in the background as well as when the app is closed.
  • Control Location tracking (start/stop/pause/continue)
  • Getting state of the service (running/binded/how many locations stored/ are permissions granted / metadata)
  • Requesting permission for GPS
  • Setting and reading metadata (might be helpfull when application were closed, but service is continue to store geolocations)
  • Getting all stored locations & only new ones

Getting Started #

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Installing #

Add service declaration to your Android manifest file, just before ending tag: "</application>".

 <service
    android:name="com.kgit.background_geolocation_plugin.service.BackgroundLocationService"
    android:enabled="true"
    android:exported="false"/>

0.1.1 #

  • Format code

0.1.0 #

  • Added MeasureState model class
  • Added more attributes to LocationItem class
  • Refactor pubspec

0.0.2 #

  • Added dart code documentation

0.0.1 #

  • TODO: Describe initial release.

example/lib/main.dart

import 'package:flutter/material.dart';

import 'package:flutter/services.dart';
import 'package:background_geolocation_plugin/background_geolocation_plugin.dart';
import 'package:background_geolocation_plugin/location_item.dart';
import 'package:background_geolocation_plugin/measure_state.dart';

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String resultMsg = 'Unknown';
  List<LocationItem> allLocations = [];
  MeasureState currentState;

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

  void execMethod(methodName) async {
    var res = "";
    try {
      if (methodName == "startLocationTracking") {
        res = await BackgroundGeolocationPlugin.startLocationTracking();
      } else if (methodName == "stopLocationTracking") {
        res = await BackgroundGeolocationPlugin.stopLocationTracking();
      } else if (methodName == "pauseLocationTracking") {
        res = await BackgroundGeolocationPlugin.pauseLocationTracking();
      } else if (methodName == "continueLocationTracking") {
        res = await BackgroundGeolocationPlugin.continueLocationTracking();
      } else if (methodName == "getState") {
        currentState = await BackgroundGeolocationPlugin.getState();
        res = currentState.toString();
      } else if (methodName == "requestPermissions") {
        res = await BackgroundGeolocationPlugin.requestPermissions();
      } else if (methodName == "getAllLocations") {
        List<LocationItem> items =
            await BackgroundGeolocationPlugin.getAllLocations();
        allLocations = items;
        res = "all stored locations size " + items.length.toString();
      } else if (methodName == "getNewLocations") {
        var time = allLocations.last.time;
        List<LocationItem> items = await BackgroundGeolocationPlugin
            .getAllStoredLocationsWithTimeBiggerThan(time);
        allLocations.addAll(items);
        res = "Got " + items.length.toString() + " new locations";
      }
    } on PlatformException catch (e) {
      res = "Error, code: " +
          e.code +
          ", message: " +
          e.message +
          ", details: " +
          e.details;
    }

    setState(() {
      resultMsg = "Result for: " + methodName + ": " + res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Column(
              children: <Widget>[
                Text(resultMsg),
                RaisedButton(
                  onPressed: () {
                    execMethod("startLocationTracking");
                  },
                  child: Text("startLocationTracking"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("stopLocationTracking");
                  },
                  child: Text("stopLocationTracking"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("pauseLocationTracking");
                  },
                  child: Text("pauseLocationTracking"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("continueLocationTracking");
                  },
                  child: Text("continueLocationTracking"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("getState");
                  },
                  child: Text("getState"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("requestPermissions");
                  },
                  child: Text("requestPermissions"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("getAllLocations");
                  },
                  child: Text("getAllLocations"),
                ),
                RaisedButton(
                  onPressed: () {
                    execMethod("getNewLocations");
                  },
                  child: Text("getNewLocations"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  background_geolocation_plugin: ^0.1.1

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:background_geolocation_plugin/background_geolocation_plugin.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
32
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
66
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform ios

Because:

  • package:background_geolocation_plugin/background_geolocation_plugin.dart that declares support for platforms: android

Package does not support Flutter platform linux

Because:

  • package:background_geolocation_plugin/background_geolocation_plugin.dart that declares support for platforms: android

Package does not support Flutter platform macos

Because:

  • package:background_geolocation_plugin/background_geolocation_plugin.dart that declares support for platforms: android

Package does not support Flutter platform web

Because:

  • package:background_geolocation_plugin/background_geolocation_plugin.dart that declares support for platforms: android

Package does not support Flutter platform windows

Because:

  • package:background_geolocation_plugin/background_geolocation_plugin.dart that declares support for platforms: android

Package not compatible with SDK dart

Because:

  • background_geolocation_plugin that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test