简体   繁体   English

手机屏幕关闭时运行HTML5移动应用程序?

[英]HTML5 mobile app running while phone screen is off?

I'm interested in creating an HTML5 geolocation-based web app that could still be operating when the phone screen is off (say, tracking how far you've been running when your phone is in your pocket). 我有兴趣创建一个基于HTML5地理位置的网络应用程序,当手机屏幕关闭时,该应用程序仍然可以运行(例如,跟踪手机放在口袋里时的运行距离)。

Is there any way to keep the app running but have the screen be off, or have the app run in the background while other apps are being used? 有没有办法让应用程序继续运行,但屏幕关闭,或者在使用其他应用程序时让应用程序在后台运行? Is this possible at least on some of the popular mobile devices out there (newer iOS and Android devices in particular?) 至少在某些流行的移动设备上是否可行(特别是较新的iOS和Android设备?)

My music app is HTML5 and also needs to run in the background. 我的音乐应用是HTML5,还需要在后台运行。 The support for that varies depending on mobile browser. 对此的支持因手机浏览器而异。

  • Safari on iOS: will continue to play one or two songs in the background iOS上的Safari:将继续在后台播放一首或两首歌曲
  • Native browser on Android: will play one song then stops Android上的原生浏览器:播放一首歌然后停止播放
  • Firefox on Android: will stop when screen locks or browser loses focus or song ends Android上的Firefox:当屏幕锁定或浏览器失去焦点或歌曲结束时将停止
  • Dolphin on Android: plays in background! 安卓上的海豚:在后台播放! but eventually stops 但最终停止了
  • Opera on Android: better background support, Javascript continues to run and music continues to play even when screen is off or Opera is sent to the background, but eventually stops after a couple songs. Android上的Opera:更好的后台支持,Javascript继续运行,音乐继续播放,即使屏幕关闭或Opera发送到后台,但最终在几首歌后停止。

As you can see it's hit or miss. 正如你所看到它的命中或错过。 Half the time I end up trying to put my phone in my pocket backwards, trying to keep the screen on, until I accidentally press it - totally sucks. 有一半时间我最终试图将手机放在我的口袋里,试图让屏幕保持开启,直到我不小心按下它 - 完全糟透了。 I long for the day when the user has more control over running HTML5 apps in the background. 我渴望用户能够更好地控制在后台运行HTML5应用程序。 If I had to guess I would say that universal support for that is very far off, if it ever even gets traction. 如果我不得不猜测我会说,对它的普遍支持是非常遥远的,如果它甚至有牵引力。 I'm being forced toward a native app solution even though I am almost positive Apple will never approve it. 即使我几乎是积极的,苹果也永远不会批准它。 In the meantime, I'll remain hopeful and keep testing the latest mobile browsers. 与此同时,我仍然充满希望并继续测试最新的移动浏览器。 Because if it actually happens it will be awesome. 因为如果真的发生它会很棒。 :-) :-)

I should also point out that, in my experience, for pretty much all of the above combinations, using HTML5 to simultaneously run javascript, pull network data, and play music will typically turn your phone into an oven and kill your battery pretty quickly. 我还应该指出,根据我的经验,对于几乎所有上述组合,使用HTML5同时运行javascript,提取网络数据和播放音乐通常会将手机变成烤箱并快速杀死电池。 Ugg. UGG。

In addition, if you are using jQuery Mobile (which is mostly fantastic), you will see different touch handling on the different browsers. 此外,如果您使用的是jQuery Mobile(这非常棒),您将在不同的浏览器上看到不同的触摸处理。 For example, Firefox touch works great, Dolphin is terrible and requires precise touch-and-hold-and-release to get right. 例如,Firefox touch效果很好,Dolphin非常糟糕,需要精确的触摸和保持 - 释放才能正确使用。 That's not directly HTML5's fault, but another issue I'm dealing with. 这不是HTML5的直接错误,而是我正在处理的另一个问题。

Here are another developer's interesting thoughts on mobile HTML5. 以下是另一位开发人员对移动HTML5的有趣想法

UPDATE: I just (May 22, 2013) downloaded Opera on my Samsung Galaxy S3 and it has the best HTML5 support so far. 更新:我刚刚(2013年5月22日)在我的三星Galaxy S3上下载了Opera,到目前为止它拥有最好的HTML5支持。 For my app, it continues to run javascript in the background, whether the screen is off, or Opera is pushed to the background, for at least a couple songs. 对于我的应用程序,它继续在后台运行javascript,无论屏幕是关闭,还是Opera被推到后台,至少有几首歌曲。

You can use Service Workers: 您可以使用服务工作者:

https://developers.google.com/web/fundamentals/primers/service-workers/ https://developers.google.com/web/fundamentals/primers/service-workers/

A service worker is a script that your browser runs in the background, separate from a web page, opening the door to features that don't need a web page or user interaction. 服务工作者是您的浏览器在后台运行的脚本,与网页分开,为不需要网页或用户交互的功能打开了大门。 Today, they already include features like push notifications and background sync. 今天,它们已经包含推送通知和后台同步等功能。 In the future, service workers might support other things like periodic sync or geofencing. 将来,服务工作者可能会支持其他内容,例如定期同步或地理围栏。

To be precise, applications can register a service worker as follows: 确切地说,应用程序可以注册服务工作者,如下所示:

if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('/sw.js').then(function(registration) {
      // Registration was successful
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function(err) {
      // registration failed :(
      console.log('ServiceWorker registration failed: ', err);
    });
  });
}

Once the service worker registration is successful, any application logic implemented in sw.js will execute in the background; 一旦服务工作者注册成功,sw.js中实现的任何应用程序逻辑将在后台执行; even if application tab is disabled. 即使禁用了应用程序选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM