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

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

问题描述

如果我关闭 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-模式

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

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,并且确实使用了 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

这是一个准备好 ES6 的 Ionic 2 示例:

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 应用程序的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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