在Ionic/Cordova应用程序后台获取位置

34

如果我关闭Ionic/Cordova应用程序(无论是iOS还是Android),是否可能运行后台服务?

出于这个目的,我选择了这个插件:https://github.com/katzer/cordova-plugin-background-mode

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

$ionicPlatform.ready(function () {
            cordova.plugins.backgroundMode.isEnabled();

            cordova.plugins.backgroundMode.configure({
                silent: true
            }) 
              ............
            ///do some task
)}

如果应用程序在前台运行时,它可以正常工作,但是一旦我关闭应用程序,正在运行的任务也会停止。

是否有任何解决方法/方法可以使我的任务在关闭应用程序时仍然运行?

编辑:

我已经为iOS和Andorid添加了权限,但结果相同。

编辑2:

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


1
你有得到任何解决方案吗? - Muhsin Keloth
你有找到解决方案吗?我也在寻找一些东西。 - Nico
3个回答

14

Ionic Framework

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

下面是能够让它工作的代码片段:

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

Ionic Framework 2

以下是一个 Ionic 2 的 ES6 示例:

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

这个功能是否可以在完全关闭应用的情况下使用? - radioaktiv
@AverageJoe 我认为这几乎是不可能的,或者你需要将值写入在重新启动后仍保留的内存中,或者使用像 NoSQL 数据库这样的另一个数据源。 - 0x1ad2
1
由于闹钟应用程序可以做到这一点,我怀疑一定有办法。我刚在我的安卓手机上尝试了一下:1)设置一个3分钟后的闹钟。2)完全关闭手机电源。3)重新启动手机,然后嘭,我设置的那个3分钟前的闹钟响了。(当时,闹钟应用程序不在“正在运行的应用程序”中,但闹钟本身仍能触发。) - Average Joe
@0x1ad2 我也在Ionic和iPhone上尝试了这段代码。我只需要将代码放在$ionicPlatform.ready(function(){})中吗?还需要其他的吗? - UniSound Waterloo
嗨..我使用了同样的插件。但是当我打开其他应用程序时,我的应用程序无法在后台工作。而且它在打开时重新启动。请让我知道是否有什么我错过的东西。提前致谢。 - AishApp
显示剩余6条评论

2
我认为你尝试实现的后台地理位置跟踪已经存在于 Cordova 插件中,它被称为 cordova-plugin-mauron85-background-geolocation

该插件是前台和后台地理位置服务。它比 HTML5 地理位置或 Cordova-geolocation 插件更省电和省流量。

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


0

关于IONIC-3的解决方法

导入插件

import { Platform } from 'ionic-angular';
import { BackgroundMode } from '@ionic-native/background-mode';

在构造函数中添加

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

如果你的应用程序已经关闭并且未在后台运行,这会起作用吗?! - Rebar
@Rebar 背景活动仅会在应用程序处于后台时发生。以上代码不支持在已停止运行或未在后台运行的应用程序中。 - Jose G Varanam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接