使用Javascript在iPad上处理待机 [英] Handling standby on iPad using Javascript

查看:112
本文介绍了使用Javascript在iPad上处理待机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,所有的好人。



我在Apple文档中搜索了一段时间关于iPad事件。然而,我没有发现任何关于如何/当iPad何时从睡眠状态改变到待机状态。



我想做的是将我的Mobile-Safari网络应用置于锁定状态,每当iPad变为非活动/待机状态,并在再次唤醒时请求PIN



这听起来很容易做,但是我似乎找不到任何线索。



希望有人尝试过类似的东西,并找到一个线索?



提前感谢

解决方案

我同意,真的应该有一些信号,你可以钩住知道应用程序何时进入睡眠状态以及唤醒时间,但是您应该可以在Safari间谍唤醒时弄清楚。



当Webview进入后台时,Safari会将其中的所有内容都放入睡眠状态。它暂停任何视频,撤销网络请求,停止更新UI并暂停所有setInterval / setTimeout操作。 JS本身永远不会意识到(据我所知)这些事情是如何发生的,但它可以说明它已经发生了。使用这种方式的最简单的方法是构建一个经常调用的方法,并检查自上次更新以来是否意外长时间。如果您希望每10秒钟更新一次,并且已经五分钟,您可以确定设备已经唤醒。以下是一个快速的例子:

  var intTime = new Date()。getTime(); 
var getTime = function(){
var intNow = new Date()。getTime();
if(intNow - intTime> 1000){
console.log(I JUST WOKE UP)
}
intTime = intNow;
setTimeout(getTime,500);
};
getTime();

这将检测用户何时从另一个选项卡返回,已关闭开发者控制台或带回Safari从背景。我把间隔设置了半秒钟;您可以将其设置为任何您需要的,尽管我认为非常低的值将出现并发问题,并可能会不必要地在设备上烧毁电池。


Good evening all good folks.

I searched for a while in the Apple documentation regarding iPad events. However I found nothing in regards of how to determine if/when the iPad is changing from an awake state to a standby state.

What I would like to do is put my Mobile-Safari web app in a locked state whenever the iPad becomes inactive/standby and ask for a PIN when it becomes awake again.

This sounds like something that would be easy to do...however I cannot seem to find any clue.

After a good amount of googling, I still haven't found anything helpful.

Hopefully someone has tried something similar and found a clue?

Thanks in advance

解决方案

I agree that there really ought to be some signal you can hook on to to know when an application goes to sleep and when it wakes up, but you should be able to figure out when Safari wakes up indirectly.

When the webview goes into the background, Safari puts everything in it to sleep. It pauses any video, defers network request, stops updating the UI and pauses all setInterval/setTimeout operations. JS itself will never be aware (as far as I can tell) how these things happened, but it can tell that it has happened. The simplest way to use this is to build a regularly invoked method and check to see if it's been an unexpectedly LONG time since the last update. If you expect something to update every 10 seconds and it's been five minutes, you can be fairly sure that the device has woken up. Here's a quick example I thought up:

    var intTime = new Date().getTime();
    var getTime = function() {
        var intNow = new Date().getTime();
        if (intNow - intTime > 1000) {
            console.log("I JUST WOKE UP")
        }
        intTime = intNow;
        setTimeout(getTime,500);
    };
    getTime();

This will detect when the user has returned from another tab, has dismissed the developer console or brought back Safari from the background. I've set the interval at half a second; you could set it at anything you need, though I think very low values will have concurrency issues and probably will burn the battery down on the device unnecessarily.

这篇关于使用Javascript在iPad上处理待机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆