background HTML5 WebApps on Android 2.3+
iOS 4.2+ allows certain开发者_运维技巧 webapps to run in the background, without any special requirements.
How is it possible to run a webapp in the background on Android (to continue playing music, for example)?
For an example, see https://coolaj86.com/demos/sandbox/html5-audio-tag.html
Begin playing the music sample on your iPod/iPad/iPhone, then click to "background" the app. Double click and swipe from left to right in the lower menu to access the player controls.
That is done by using background Services.
As Robby said, to do this you need to use a Service.
Obviously you can't have a "background web page" just like you can't have one of those with any browser. The solution is to use WebView's setJavascriptInterface() to create an interface between your background service and HTML5/JS front-end in the WebView. With a javascript interface, you can create your own bindings to call into Java code from inside of your javascript.
The service can be used to preserve the background functionality, but when the app comes back into the foreground the WebView needs to bind to it via javascript interface again, get the data it needs, and populate its UI.
This is not really an answer, but wouldn't it be great though, if we could request a webpage to continue running in the background. Of course the page should need an explicit (and at all time revokable) consent from the user, but that doesn't seem like a completely impossible thing to handle for the (native) browsers? :( We already prompts the users for permission to use other APIs like the location API - why not also a run-as-a-service API?
It would be such a huge step forward for all webapps to be equal to native apps in my opinion! <3
精彩评论