在Ionic/Cordova应用中获取位置

时间:2022-03-09 15:54:01

Is it possible to run a background service if I close my Ionic/Cordova app (both for iOS and Android) ?

如果我关闭Ionic/Cordova应用(iOS和安卓系统),是否有可能运行后台服务?

For the purpose I picked that pluging https://github.com/katzer/cordova-plugin-background-mode

为此,我选择了插入https://github.com/katzer/cordova-plugin- backmode

So far I have that code:

到目前为止,我有这个代码:

$ionicPlatform.ready(function () {            cordova.plugins.backgroundMode.isEnabled();            cordova.plugins.backgroundMode.configure({                silent: true            })               ............            ///do some task)}

It works fine if the app goes to the foreground but as soon as I close the application the task I am running stops as well. So is there any workaround/way to make my task running even if the app is closed ?

如果应用程序转到前台,它可以正常工作,但是当我关闭应用程序时,我正在运行的任务也会停止。那么,有没有什么方法可以让我的任务运行呢?

EDIT:

编辑:

I have also added permissions for both iOS and Andorid but I am getting the same result.

我也增加了iOS和Andorid的权限,但是我得到了相同的结果。

EDIT 2:

编辑2:

What am I trying to do in background is to write my own implementation of significant location-change service since there is no free plugin for Cordova or PhoneGap that can work with both iOS and Android.

在后台,我要做的是编写一个重要的位置更改服务的实现,因为没有可以与iOS和Android兼容的免费Cordova或PhoneGap插件。

3 个解决方案

#1


13  

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).

我最近在我的项目中实现了一个这样的特性。我使用了Ionic和Cordova插件背景模式。(现在我正在通过ios9.2模拟器运行后台进程)。

Here's a code snippet to get it working:

下面是一个让它工作的代码片段:

// Run when the device is readydocument.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

离子框架2

Here's an Ionic 2 example ES6 ready:

这里有一个离子2的例子ES6准备好了:

// Import the Ionic Native plugin import { BackgroundMode } from 'ionic-native';// Run when the device is readydocument.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);

#2


2  

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.

我认为你正在尝试实现的后台地理定位跟踪已经作为一个cordova插件存在,它叫做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.

这个插件是一个前台和后台地理定位服务。它比html5 geolocation或cordova-geolocation插件更节省电池和数据。

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

有很多配置选项,请参见上面链接的github页面。

#3


0  

Workaround on IONIC-3

解决方案在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            });        }    })}

#1


13  

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).

我最近在我的项目中实现了一个这样的特性。我使用了Ionic和Cordova插件背景模式。(现在我正在通过ios9.2模拟器运行后台进程)。

Here's a code snippet to get it working:

下面是一个让它工作的代码片段:

// Run when the device is readydocument.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

离子框架2

Here's an Ionic 2 example ES6 ready:

这里有一个离子2的例子ES6准备好了:

// Import the Ionic Native plugin import { BackgroundMode } from 'ionic-native';// Run when the device is readydocument.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);

#2


2  

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.

我认为你正在尝试实现的后台地理定位跟踪已经作为一个cordova插件存在,它叫做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.

这个插件是一个前台和后台地理定位服务。它比html5 geolocation或cordova-geolocation插件更节省电池和数据。

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

有很多配置选项,请参见上面链接的github页面。

#3


0  

Workaround on IONIC-3

解决方案在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            });        }    })}