当活动处于后台时,Android WebView不一致

Android WebView inconsistent when activity is in the background

本文关键字:Android WebView 不一致 后台 活动 于后台      更新时间:2024-07-03

我使用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