【问题标题】:Why Cordova resume event is not firing in power lock mode / Sleep mode in iOS using sencha为什么 Cordova 恢复事件不会在 iOS 中使用 sencha 在电源锁定模式/睡眠模式下触发
【发布时间】:2016-03-30 21:49:20
【问题描述】:

在我的应用程序中,如果用户锁定了手机。我需要导航到登录屏幕。如果用户解锁设备,我已经实现了恢复事件来导航登录屏幕。谁能告诉为什么 Cordova 恢复事件在电源锁定模式下没有触发/ iOS 中的睡眠模式

在锁定模式下我还需要使用其他事件吗?

P.S 它正在最小化应用程序和最大化应用程序

【问题讨论】:

  • 按下电源键时您还在应用程序中吗?仅当从后台检索应用程序时才会触发恢复,因此当您仍在应用程序中时,尽管您处于锁定模式但在此期间应用程序尚未进入后台,但从不会触发恢复。按下电源按钮后是否触发了暂停事件(进入锁定模式)?
  • @Blauharley 没有我需要在电源锁定模式下使用什么事件
  • 不幸的是,没有更多事件可以使用,并且暂停事件应该在应用程序被搁置时触发,因此在您的情况下,应用程序仍然在前台处于活动状态,因为没有触发暂停并且因此仍然能够进行活动/非活动检查。此 IOS 行为与 android 不同,因此您可能必须为这两个平台实施解决方案。
  • @Blauharley 你能告诉我如何在 java 脚本或 sencha 中检查活动或非活动应用程序

标签: ios iphone cordova sencha-touch sencha-architect


【解决方案1】:

虽然在 IOS 中通过按下主页按钮最小化或最大化应用程序时会触发恢复事件,但至少在按下电源按钮“关闭”或“启动”应用程序时似乎不会触发恢复事件IOS。

一个可能的 JS 解决方案可能是检查不活动。假设应用程序在一段时间内没有收到用户触发的任何事件(30 秒,并且如果此后没有触发真正的暂停事件),例如点击/触摸事件,那么可以假设应用程序仍然可以执行一些代码(所以它仍然在前台)并“暂停”:

// threshold for inactivity state
var idleTimeout = 30000;
// variable that holds the time in seconds, which indicates how long the app has not received certain events
var timeInSecondsPassed = 0;
// interval instance
var intervalInstance = null;
// variable to handle the transition from "pause" to "resume" state
var inPauseState = false;

function startPauseListener() {
    timeInSecondsPassed = 0;
    var resetPassedTime = function(){
        timeInSecondsPassed = 0;
        // has the app reached the "pause" state and 
        // currently receiving certain events -> the "resume" state is reached
        if(inPauseState){
           inPauseState = false;
           // the "resume" state is reached here 
           // so the same code might be executed here as it is in the resume-listener
        }
    };
    document.ontouchstart = resetPassedTime;
    document.onclick = resetPassedTime;
    document.onscroll = resetPassedTime;
    document.onkeypress = resetPassedTime;
    intervalInstance = setInterval(checkPauseState,1000);
}

function clearPauseListener() {
    clearInterval(intervalInstance);
    timeInSecondsPassed = 0;
}

function checkPauseState() {
    timeInSecondsPassed += 1000;
    if (timeInSecondsPassed >= idleTimeout) {
       inPauseState = true;
       timeInSecondsPassed = 0;
       // run further code here to handle "pause" state
       // at this point it is assumed as soon as the app receives click/touch-events again a "resume" state is reached.
    }
}

function onDeviceReady() {
    // handle android devices so that the interval is stopped when a real pause event is fired and started when a real resume event is fired
    document.addEventListener("resume", function(){
        startPauseListener();
        // your actual code to handle real resume events
    }, false);

    document.addEventListener("pause", function(){
        clearPauseListener();
    }, false);
}

必须注意的是,当应用程序真正暂停时,会触发暂停事件,上面的代码不是在 IOS 中运行,而是在 android 中运行,这就是为什么您可能必须通过利用两者来以不同方式在 android 中处理这种情况android 中的 resume 和 pause-Listener 用于当应用程序被主页按钮最小化时,间隔仍将被执行并在后台消耗 CPU。

另外请注意,这只是一种概念代码,并未在任何设备上测试!!!

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    有一个名为 active 的 iOS 特定事件“检测用户何时禁用锁定按钮以在应用在前台运行的情况下解锁设备”。

    查看resume doc 页面底部的文档:

    https://cordova.apache.org/docs/en/5.1.1/cordova/events/events.resume.html

    【讨论】:

      猜你喜欢
      • 2012-03-04
      • 2011-05-01
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多