background_locator 0.0.4-beta
background_locator: ^0.0.4-beta copied to clipboard
A Flutter plugin for updating location in background.
background_locator #
A Flutter plugin for updating location in background.
Install #
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies:
background_locator: ^0.0.4-beta
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_locator/background_locator.dart';
Setup #
Android #
- Add the following permission to
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
- Add the following lines to your
AndroidManifest.xmlto register the Services and BroadcastReceiver:
<receiver android:name="rekab.app.background_locator.LocatorBroadcastReceiver"
android:enabled="true"
android:exported="true"/>
<service android:name="rekab.app.background_locator.LocatorService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
<service android:name="rekab.app.background_locator.IsolateHolderService"
android:permission="android.permission.FOREGROUND_SERVICE"
android:exported="true"
/>
- Create an
Applicationclass and add the following lines:
class Application: FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
LocatorService.setPluginRegistrant(this)
}
override fun registerWith(p0: PluginRegistry?) {
GeneratedPluginRegistrant.registerWith(p0)
}
}
Note: Don't forget to reference your Application class in AndroidManifest.xml:
<application
android:name=".Application"
iOS #
- Add the following lines to
AppDelegateclass:
import background_locator
func registerPlugins(registry: FlutterPluginRegistry) -> () {
GeneratedPluginRegistrant.register(with: registry)
}
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
BackgroundLocatorPlugin.setPluginRegistrantCallback(registerPlugins)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
-
In app setting enable
Background Modesand checkLocation Updates. -
In
Info.plistadd Key for using location service:
NSLocationAlwaysAndWhenInUseUsageDescription
NSLocationWhenInUseUsageDescription
Usage #
- Initialize plugin:
@override
void initState() {
super.initState();
IsolateNameServer.registerPortWithName(port.sendPort, 'LocatorIsolate');
port.listen((dynamic data) {
// do something with data
});
initPlatformState();
}
Future<void> initPlatformState() async {
await BackgroundLocator.initialize();
}
- Call
BackgroundLocator.registerLocationUpdate(callback);with similar callback:
static void callback(LocationDto locationDto) async {
final SendPort send = IsolateNameServer.lookupPortByName('LocatorIsolate');
send?.send(locationDto);
}
- Don't forget to unregister the locator when you are done:
BackgroundLocator.unRegisterLocationUpdate();