在后台获取Ionic / Cordova app中的位置 [英] Get location in Ionic/Cordova app when in background

查看:802
本文介绍了在后台获取Ionic / Cordova app中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我关闭Ionic / Cordova应用程序(适用于iOS和Android),是否可以运行后台服务?

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

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

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

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 ?

编辑:

我还为iOS和Andorid添加了权限但是我得到了相同的结果。

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

编辑2:

我在后台尝试做的是编写我自己的重要位置更改服务实现,因为没有适用于iOS和Android的Cordova或PhoneGap的免费插件。

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.

推荐答案

Ionic Framework

I我最近在我的项目中实现了这样的功能。我确实使用了Ionic,我确实使用了Katzer的Cordova插件背景模式。 (现在我正在通过iOS 9.2模拟器运行后台进程)。

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

离子框架2

这是一个Ionic 2示例ES6准备就绪:

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

这篇关于在后台获取Ionic / Cordova app中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆