我有一个android phonegap cordova gps跟踪系统,它根据cordova应用程序上php文件上的数据库(名称、用户和密码)将位置发送到远程服务器数据库。
我的问题是:
如何让我的应用程序启动或作为后台服务,一步一步地简单,请我不专业:-)
发布于 2016-02-23 01:25:30
背景Cordova GeoLocation:
首先,您需要添加一个背景地理定位插件,它帮助您完成您需要在应用程序中添加的服务类型。
使用插件
插件使用以下方法创建对象window.plugins.backgroundGeoLocation
configure(success, fail, option),
start(success, fail)
stop(success, fail).
安装插件:
科多瓦插件添加https://github.com/christocracy/cordova-plugin-background-geolocation.git艾奇通!如果不使用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从回购程序中复制到任意文件夹(如: 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
欲了解更多信息,请单击以下链接:
干杯,快乐编码。
https://stackoverflow.com/questions/35565868
复制