【问题标题】:Get location in Ionic/Cordova app when in background在后台获取 Ionic/Cordova 应用程序的位置
【发布时间】:2016-01-11 18:04:05
【问题描述】:

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

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

【问题讨论】:

  • 你有解决办法吗?
  • 你有什么解决办法吗?同样在这里找东西

标签: cordova ionic-framework background background-service


【解决方案1】:

Ionic Framework

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

这里有一个代码 sn-p 让它工作:

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

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

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

【讨论】:

  • 即使您完全关闭应用程序也能正常工作?
  • 如果您像“将其移至后台”一样关闭应用程序,它将起作用。
  • 如果这个人关掉手机(我的意思是完全关机并重新启动),那么将你的代码放回游戏需要什么?我知道所有的闹钟应用都能做到这一点。
  • @AverageJoe 我认为这几乎是不可能的,或者您需要将值写入重新启动后保留的内存或 NoSQL 数据库等其他数据源。
  • 既然报警应用可以做到这一点,我怀疑一定有办法。我刚在我的安卓手机上试过这个; 1)设置警报3分钟以备后用。 2) 将手机完全关机。 3)然后启动它然后繁荣,我3分钟前设置的警报启动了。(当时,警报应用程序不在“正在运行的应用程序”中,但警报本身能够触发。)
【解决方案2】:

我认为您尝试实现的后台地理定位跟踪已经作为cordova插件存在,它被称为cordova-plugin-mauron85-background-geolocation

这个插件既是前台也是后台的地理定位服务。与 html5 地理定位或 cordova-geolocation 插件相比,它的电池和数据效率要高得多。

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

【讨论】:

    【解决方案3】:

    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 后台活动仅在应用程序处于后台时才会发生。上面的代码不支持被杀死的应用程序或不在后台运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2015-09-21
    相关资源
    最近更新 更多