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

时间:2015-03-01 04:10:10

标签: java javascript android google-chrome android-webview

我正在使用WebView通过JavaScript接收Google Cloud频道消息(请参阅下面的代码)。 WebView托管在登录期间使用的Activity中,然后保存在后台。

compileSdkVersion< 19(总是使用Android的WebView) - >即使活动在后台,JavaScript也会继续运行并接收事件。

compileSdkVersion> = 19(使用基于Chrome的WebView) - >在SDK> = 19的合适设备上,即使活动在后台,JavaScript也会继续运行并接收事件,旧设备上的 BUT SDK< 19,一旦另一个活动开始,就不会收到任何事件。

尽管Google已宣布http://www.zdnet.com/article/google-why-we-wont-patch-pre-kitkat-android-webview/不在旧版设备上修复WebView的XSS漏洞,但在将compileSdkVersion设置为19或更高版本时,我认为Google 搞乱了旧版设备。较旧的设备似乎以某种兼容模式运行,这似乎与预期的行为不兼容。谷歌谈到升级WebView https://developer.android.com/guide/webapps/migrating.html,但没有提到WebView在旧设备上的表现不同。我也不相信这是故意的。我读过Google在Lollypop上做了进一步的更改,但我没有可用于测试的设备。

换句话说,我们在Android中预期会出现一个大的不一致的beta混乱。任何人都有使用compileSdkVersion> = 19的旧设备后台工作的WebView或知道解决方法吗?

这是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(); }
    }

}

1 个答案:

答案 0 :(得分:0)

关于棒棒糖:

  

我读过Google在Lollypop上做了进一步的更改,但我没有一台可以用来测试的设备。

我们使用基于Chromium的WebView进行的测试表明App Engine Channel不适用于Android版本5.02(Lollipop API等级21)。

compileSdkVersion 22,buildToolsVersion&#34; 22.0.1&#34;,minSdkVersion 17,targetSdkVersion 22

提出了以下问题:

https://code.google.com/p/android/issues/detail?id=172262

https://code.google.com/p/chromium/issues/detail?id=485749