将我的phonegap cordova gps跟踪系统转为启动或作为服务工作

时间:2016-02-22 23:20:57

标签: android cordova gps phonegap-plugins tracking

我有一个android phonegap cordova gps跟踪系统,它根据cordova App上php文件中的数据库(名称,用户和密码)将位置发送到远程服务器数据库。

  • 服务器端由php编写,其确定。
  • 我的问题是,如果我回到App,它会停止发送位置,因为它无法在后台运行。

我的问题是:

如何以简单的方式将我的应用程序转为启动或作为后台服务 请一步一步请我不专业: - )

1 个答案:

答案 0 :(得分:0)

背景Cordova GeoLocation:

首先需要做的是,您需要添加backgroudgeolocation plugin,这有助于您在应用程序中添加需要的服务。

使用插件

该插件使用方法

创建对象window.plugins.backgroundGeoLocation
configure(success, fail, option),

start(success, fail)

stop(success, fail).

安装插件:

cordova插件添加https://github.com/christocracy/cordova-plugin-background-geolocation.git ACHTUNG!如果您不使用Cordova 5.x,则必须使用标记分支#cordova-4.x来安装插件,如下所示(因为Cordova已迁移到npm)

cordova插件添加https://github.com/christocracy/cordova-plugin-background-geolocation.git#cordova-4.x 一个完整的例子可能是:

//
//
// after deviceready
//
//

// Your app must execute AT LEAST ONE call for the current position via standard Cordova geolocation,
//  in order to prompt the user for Location permission.
window.navigator.geolocation.getCurrentPosition(function(location) {
    console.log('Location from Phonegap');
});

var bgGeo = window.plugins.backgroundGeoLocation;

/**
* This would be your own callback for Ajax-requests after POSTing background geolocation to your server.
*/
var yourAjaxCallback = function(response) {
    ////
    // 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.
    //
    //
    bgGeo.finish();
};

/**
* This callback will be executed every time a geolocation is recorded in the background.
*/
var callbackFn = function(location) {
    console.log('[js] BackgroundGeoLocation callback:  ' + location.latitude + ',' + location.longitude);
    // Do your HTTP request here to POST location to your server.
    //
    //
    yourAjaxCallback.call(this);
};

var failureFn = function(error) {
    console.log('BackgroundGeoLocation error');
}

// BackgroundGeoLocation is highly configurable.
bgGeo.configure(callbackFn, failureFn, {
    url: 'http://only.for.android.com/update_location.json', // <-- Android ONLY:  your server url to send locations to
    params: {
        auth_token: 'user_secret_auth_token',    //  <-- Android ONLY:  HTTP POST params sent to your server when persisting locations.
        foo: 'bar'                              //  <-- Android ONLY:  HTTP POST params sent to your server when persisting locations.
    },
    headers: {                                   // <-- Android ONLY:  Optional HTTP headers sent to your configured #url when persisting locations
        "X-Foo": "BAR"
    },
    desiredAccuracy: 10,
    stationaryRadius: 20,
    distanceFilter: 30,
    notificationTitle: 'Background tracking', // <-- android only, customize the title of the notification
    notificationText: 'ENABLED', // <-- android only, customize the text of the notification
    activityType: 'AutomotiveNavigation',
    debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
    stopOnTerminate: false // <-- enable this to clear background location settings when the app terminates
});

// Turn ON the background-geolocation system.  The user will be tracked whenever they suspend the app.
bgGeo.start();

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

注意:该插件包含org.apache.cordova.geolocation作为依赖项。您必须在前台启用Cordova的GeoLocation,并让用户通过执行#watchPosition或#getCurrentPosition接受位置服务。

首先,将SampleApp从repo中复制到您想要的任何文件夹中(例如:tmp)。

$ cp -R cordova-plugin-background-geolocation/example/SampleApp ./tmp
$ cd tmp/SampleApp
$ cordova plugin add https://github.com/christocracy/cordova-plugin-background-geolocation.git
$ cordova platform add ios
$ cordova build ios

有关更多信息,请单击以下链接:

Cordova background-geolocation

干杯,快乐的编码。