【问题标题】:Repeatedly background task ionic framework重复后台任务离子框架
【发布时间】:2015-08-18 10:06:16
【问题描述】:

我正在使用 ionic-framework / cordova 来构建我的应用程序。 我已经安装了这个插件cordova-plugin-background-mode 在后台模式下工作。

问:但是现在我怎样才能在后台执行每 30 分钟重复一次的任务呢?

你知道另一个插件可以做到这一点吗?

谢谢

【问题讨论】:

    标签: android cordova ionic-framework ionic cordova-plugins


    【解决方案1】:

    根据您使用的存储库,代码必须如下所示:

    // 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 30 minutes (1800000 milliseconds)
            setInterval(function () {
    
                // The code that you want to run repeatedly
    
            }, 1800000);
        }
    }, false);
    

    【讨论】:

      【解决方案2】:

      您可以使用$timeout$interval 执行此任务,并有一定的时间段

      Angular doc for interval

      【讨论】:

      • 当 ionic/cordova 应用程序在设备的后台时,所有 Angular 代码都会暂停。虽然$timeout$interval 是应用在前台时的正确答案,但在设备上运行时却无济于事。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 2018-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      相关资源
      最近更新 更多