[New Feature] Android Headless Mode

React Native

For React Native users, you get the best implementation because the plugin implements the React Native Headless JS mechanism. This allows you to implement your “Headless Task” in the Javascript environment. No native Android code required for you!

Try it out

With your React Native debugger connected, terminate your app and observe log messages arriving from your HeadlessTask.

Cordova / Ionic

For Cordova users, unfortunately the Cordova platform provides no such mechanism to execute Javascript code in the Headless state, so you’ll have to implement your Headless code in the native Android environment with Java code. Don’t be daunted — it’s really not that difficult.

Step 1

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:

Step 2

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:

Step 3

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.

--

--

Creator of Background Geolocation SDK. Professional plugin developers — geolocation specialists. https://www.transistorsoft.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store