如何使用ionic backgroundgeolocation插件API完成?

时间:2018-08-28 06:15:32

标签: ionic-framework cordova-plugins

我不太了解ionic backgroundgeoloaction插件的API stop()和finish()之间的区别。如下面的文档描述:finish()仅用于iOS。我是否需要始终将其添加到iOS应用程序的configure()函数中?

import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation';

constructor(private backgroundGeolocation: BackgroundGeolocation) { }

...

const config: BackgroundGeolocationConfig = {
            desiredAccuracy: 10,
            stationaryRadius: 20,
            distanceFilter: 30,
            debug: true, //  enable this hear sounds for background-geolocation life-cycle.
            stopOnTerminate: false, // enable this to clear background location settings when the app terminates
    };

this.backgroundGeolocation.configure(config)
  .subscribe((location: BackgroundGeolocationResponse) => {

    console.log(location);

    // IMPORTANT:  You must execute the finish method here to inform the native plugin that you're finished,
    // and the background-task may be completed.  You must do this regardless if your HTTP request is successful or not.
    // IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
    this.backgroundGeolocation.finish(); // FOR IOS ONLY

  });

// start recording location
this.backgroundGeolocation.start();

// If you wish to turn OFF background-tracking, call the #stop method.
this.backgroundGeolocation.stop();

1 个答案:

答案 0 :(得分:1)

stop()函数

此功能停止插件。它就像一个开关。当用户停用此功能或不再需要此功能时,可以使用它。

finish()函数

此功能向OS指示任务(获取当前位置)已完成。由于该插件仍在运行,因此以后将执行其他类似的任务(直到下次需要定位时,可能是几秒钟后)。对于每个任务,您必须向OS指示任务完成的时间。

如果没有该函数调用,IOS将使与插件的连接保持打开状态。因此,如文档中所述,对于IOS应用程序,您必须始终在此插件的回调函数中使用它。

如果我不够清楚,请告诉我。