当活动处于后台时,Android WebView不一致
Android WebView inconsistent when activity is in the background
我使用WebView来接收使用JavaScript的Google Cloud频道消息(请参阅下面的代码)。WebView托管在登录期间使用的"活动"中,然后将其保留在后台。
compileSdkVersion<19(始终使用Android的WebView)->JavaScript保持运行并接收事件,即使"活动"在后台。
compileSdkVersion>=19(使用基于Chrome的WebView)->在具有SDK>=19的合适设备上,即使"活动"在后台,JavaScript也会继续运行并接收事件,BUT在旧设备上SDK<19,一旦启动另一个"活动",就不会收到任何事件。
尽管谷歌已经宣布http://www.zdnet.com/article/google-why-we-wont-patch-pre-kitkat-android-webview/不是为了修复旧设备上WebView的XSS漏洞,在我看来,当compileSdkVersion设置为19或更高版本时,谷歌似乎在干扰旧设备。较旧的设备似乎以某种兼容模式运行,这似乎与预期行为不兼容。谷歌谈升级WebViewhttps://developer.android.com/guide/webapps/migrating.html,但没有提到WebView在旧设备上的行为有所不同。我也不相信这是故意的。我读到谷歌对Lollypop做了进一步的更改,但我没有可以用来测试的设备。
换句话说,一个巨大的不一致的测试混乱,这是我们在安卓系统中都预料到的。有人让WebView在compileSdkVersion>=19的旧设备上后台工作,或者知道解决方法吗?
这是html代码:
<html>
<head>
<script src='{{ channelurl }}jsapi'></script>
</head>
<body>
<div>WebView for ChannelService</div>
<script type="text/javascript">
onOpen = function() {
ChannelListener.onOpen();
};
onMessage = function(message) {
ChannelListener.onMessage(message.data);
};
onError = function(error) {
ChannelListener.onError(error.code, error.description);
};
onClose = function() {
ChannelListener.onClose();
};
var token = '{{ token }}';
var channel = new goog.appengine.Channel(token);
var handler = {
'onopen': onOpen,
'onmessage': onMessage,
'onerror': onError,
'onclose': onClose
};
var socket = channel.open(handler);
socket.onopen = onOpen;
socket.onmessage = onMessage;
socket.onerror = onError;
socket.onclose = onClose;
</script>
</body>
</html>
以及相关的Java代码:
public class ChannelService {
private class ChannelListenerJavascriptInterface { // receive channel message from App Engine
@JavascriptInterface public void onOpen() {
requestOpen();
}
@JavascriptInterface public void onMessage(String message) {
requestMessage(message);
}
@JavascriptInterface public void onError(Integer code, String description) {
requestError(code, description);
}
@JavascriptInterface public void onClose() {
requestClose();
}
}
public ChannelService() throws IOException { // use a hidden WebView to host JavaScript code
if (Session.exists()) {
final Activity activity = (Activity) Session.getContext(); // retrieve the activity context in which the web view is hosted
activity.runOnUiThread(new Runnable() { // ensure WebView is running on UI thread
@Override public void run() {
WebView webView = (WebView) activity.findViewById(R.id.channel_webview); // listen to remote channel while Session exists; otherwise only to local channels
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new ChannelListenerJavascriptInterface(), "ChannelListener");
String html = null;
try {
html = AssetsHelper.assetToString(Session.getContext(), "channel.html");
} catch (IOException e) {
throw new RuntimeException("Cannot read assets/channel.html file.", e);
}
html = html.replaceAll("''{''{ channelurl ''}''}", MobileService.getChannelEndpoint());
html = html.replaceAll("''{''{ token ''}''}", Session.getId());
webView.loadData(html, "text/html", "UTF-8");
}
});
}
}
// Callback
private OnMessageListener onMessageListener; public void setOnMessageListener(OnMessageListener onMessageListener) { this.onMessageListener = onMessageListener; }
public interface OnMessageListener {
public void onOpen();
public void onMessage(String message);
public void onClose();
public void onError(Integer errorCode, String description);
}
private void requestOpen() {
if(onMessageListener != null) { onMessageListener.onOpen(); }
}
private void requestMessage(String message) {
if(onMessageListener != null) { onMessageListener.onMessage(message); }
}
private void requestError(Integer code, String description) {
if(onMessageListener != null) { onMessageListener.onError(code, description); }
}
private void requestClose() {
if(onMessageListener != null) { onMessageListener.onClose(); }
}
}
关于棒棒糖:
我读到谷歌对Lollypop做了进一步的更改,但我没有可以用来测试的设备。
我们对基于Chromium的WebView的测试表明,应用程序引擎频道不适用于Android版本5.02(Lollipop API 21级)。
compileSdkVersion 22,buildToolsVersion"22.0.1",minSdkVersion 17,targetSdkVersion 22
提出了以下问题:
https://code.google.com/p/android/issues/detail?id=172262
https://code.google.com/p/chromium/issues/detail?id=485749
- window.onload没有'无法在Android WebView中工作
- Android Webview通过Javascript注入CSS
- 在Android WebView中通过javascript检测点击HTML选项标记
- 用JavaScript替换Android WebView模板上的html文本
- Hammer.js阻止在Android Webview中点击超链接
- 在Android WebView中运行Javascript-onPageFinished循环
- Chrome 50的Android WebView在触摸页面之前会阻止Javascript
- HTML5画布支持和Android Webview
- 带有javascript的HTML没有加载到android webview中
- 带有iframe嵌入的Android WebView;t显示在4.2.2及以下
- 如果没有互联网连接,Android WebView离线消息
- Android Webview没有'有时不会加载HTML
- 如何在Android WebView中嵌入Youtube视频
- 检查登录错误Android WebView Javascript
- Injecting JQuery into Android Webview
- 为什么可以't我在Android WebView onLayout中设置了最大宽度
- Android Webview触摸内容
- 了解 Android webview JavaScript 界面
- 控制在 Android WebView 中执行的 JS
- 在 Android WebView 中过滤 DOM 元素,而无需 jQuery