Skip to content

Android Headless Mode

Chris Scott edited this page Jun 27, 2018 · 7 revisions

BackgroundGeolocation implements the "Headless Javascript" mechanism. With Headless Javascript, you can continue to respond to BackgroundGeolocation events after your app has been terminated (assuming you've configured stopOnTerminate: false).

Step 1: enableHeadless: true.

Where you execute BackgroundGeolocation#ready, add the following options:

import {BackgroundGeolocation} from "nativescript-background-geolocation-lt";

export class HelloWorldModel extends Observable {
  constructor() {
    
    BackgroundGeolocation.ready({
      enableHeadless: true,    // <-- Enable Android Headless mode
      stopOnTerminate: false,  // <-- required for Headless JS
      .
      .
      .
    }, (state) => {
      console.log('- Configure success');
    });

Step 2: BackgroundGeolocation#registerHeadlessTask

In your application's app.ts file, register your Javascript Headless Task:

📂 app/app.ts

import * as application from 'application';

import {BackgroundGeolocation} from "nativescript-background-geolocation-lt";
.
.
.
if (application.android) {
  BackgroundGeolocation.registerHeadlessTask((event, completionHandler) => {
    console.log('[My BackgroundGeolocation Headless Task]', event.name, event.params);
    // Do stuff.
    completionHandler();  // <-- signal completion of your HeadlessTask
  });
}
application.run({ moduleName: 'app-root' });
Clone this wiki locally