在我的应用程序中,如果用户锁定了手机。我需要导航到登录屏幕。如果用户解锁设备,我已经实现了恢复事件来导航登录屏幕。有人能告诉为什么Cordova恢复事件没有在iOS 中的电源锁定模式/睡眠模式下启动吗
我需要在锁定模式下使用其他事件吗?
p.S它正在最小化应用程序并最大化应用程序
尽管在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中通过利用resume和pause Listener来不同地处理这个szenario。因为在android中,当应用通过主页按钮最小化时,间隔仍然会执行,并在后台消耗CPU。
还请注意,这只是一种概念代码,没有在任何设备中测试!!!
希望这能有所帮助。
有一个名为active
的iOS特定事件,它"检测用户何时禁用锁定按钮以在前台运行应用程序的情况下解锁设备"。
检查resume
文档页面底部的文档:
https://cordova.apache.org/docs/en/5.1.1/cordova/events/events.resume.html