Get location in Ionic/Cordova app when in background

别来无恙 提交于 2019-11-28 18:37:31

Ionic Framework

I've recently implemented a feature like this in my project. I did use Ionic and I did use the Cordova plugin background mode from Katzer. (right now I'm running the background process through the iOS 9.2 simulator).

Here's a code snippet to get it working:

// Run when the device is ready
document.addEventListener('deviceready', function () {

    // Android customization
    // To indicate that the app is executing tasks in background and being paused would disrupt the user.
    // The plug-in has to create a notification while in background - like a download progress bar.
    cordova.plugins.backgroundMode.setDefaults({ 
        title:  'TheTitleOfYourProcess',
        text:   'Executing background tasks.'
    });

    // Enable background mode
    cordova.plugins.backgroundMode.enable();

    // Called when background mode has been activated
    cordova.plugins.backgroundMode.onactivate = function () {

        // Set an interval of 3 seconds (3000 milliseconds)
        setInterval(function () {

            // The code that you want to run repeatedly

        }, 3000);
    }
}, false);

Ionic Framework 2

Here's an Ionic 2 example ES6 ready:

// Import the Ionic Native plugin 
import { BackgroundMode } from 'ionic-native';

// Run when the device is ready
document.addEventListener('deviceready', () => {

    // Android customization
    // To indicate that the app is executing tasks in background and being paused would disrupt the user.
    // The plug-in has to create a notification while in background - like a download progress bar.
    BackgroundMode.setDefaults({ 
        title:  'TheTitleOfYourProcess',
        text:   'Executing background tasks.'
    });

    // Enable background mode
    BackgroundMode.enable();

    // Called when background mode has been activated
    // note: onactive now returns an returns an observable that emits when background mode is activated
    BackgroundMode.onactivate.subscribe(() => {
          // The code that you want to run repeatedly
    });
}, false);

I think the background geolocation tracking that you're trying to implement already exists as a cordova plugin, it's called cordova-plugin-mauron85-background-geolocation.

This plugin is both a foreground and background geolocation service. It is far more battery and data efficient then html5 geolocation or cordova-geolocation plugin.

There are a lot of configuration options, see the github page linked above.

Workaround on IONIC-3

import the plugins

import { Platform } from 'ionic-angular';
import { BackgroundMode } from '@ionic-native/background-mode';

add in constructor

constructor(private backgroundMode: BackgroundMode, private plt: Platform) {
    this.initBackgroundMode();
}

private initBackgroundMode() {
    this.plt.ready().then(() => {
        this.backgroundMode.setDefaults({ silent: true });
        this.backgroundMode.enable();
        if (this.plt.is("android")) {
            this.backgroundMode.on('activate').subscribe(() => {
                this.backgroundMode.disableWebViewOptimizations();
                // Custom code for updating the location 
                // or do similar functionality
                // use timeout or interval accordingly to execute the functionality in loop
            });
        }
    })
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!