[New Feature] Background Geolocation "Headless Mode" for Android Posted on 04 Feb 16:25 , 0 comments
But what if you need to implement some custom business logic in the "headless" state, such as posting a local notification or executing #getCurrentPosition in a heartbeat event?
The BackgroundGeolocation plugin for React Native and Cordova now provides a mechanism for you to implement your own custom code to handle all the plugin's events while in the "Headless" state (heartbeat, location, motionchange, geofence, etc).
Try it out
With your React Native debugger connected, terminate your app and observe log messages arriving from your HeadlessTask.
For more information, see the React Native Background Geolocation Wiki Android Headless Mode.
Cordova / Ionic
First, create a Java file named BackgroundGeolocationHeadlessTask.java in your src folder (eg: src/android/BackgroundGeolocationHeadlessTask.java). This file must extend HeadlessTask and implement HeadlessTask.Receiver exactly as shown below:
Next, you need to instruct Cordova to copy your custom source-file file into the background-geolocation plugin's src-tree. In your Cordova config.xml, add the following element to copy your custom source-file:
Finally, configure the plugin with enableHeadless: true:
Try it out
While observing $ adb logcat, terminate your app and observe log messages arriving from your custom BackgroundGeolocationHeadlessTask. With just a small handful of Java code, you're now receiving all events emitted by the plugin while in the Headless state with your app terminated.
For more information, see the Cordova Background Geolocation Wiki Android Headless Mode.