首页 文章

在后台获取Ionic / Cordova app中的位置

提问于
浏览
32

如果我关闭我的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
)}

如果应用程序转到前台,它可以正常工作,但是一旦我关闭应用程序,我运行的任务也会停止 . 那么即使应用程序关闭,有没有任何解决方法/方法可以让我的任务运行?

EDIT:

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

EDIT 2:

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

3 回答

  • 13

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

    我认为你're trying to implement already exists as a cordova plugin, it'的背景地理位置跟踪称为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
                });
            }
        })
    }
    

相关问题