问题
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 http://sandbox.coolaj86.info/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.
回答1:
That is done by using background Services.
回答2:
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.
来源:https://stackoverflow.com/questions/5331245/background-html5-webapps-on-android-2-3