本文介绍了如何处理Facebook的喜欢与确认机器人的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现像Facebook的使用Android的web视图功能。这是工作的罚款没有确认对话框。但它不工作的时候喜欢需要确认。下面是我使用的code段。

I am trying to implement facebook like functionality using android webview. It is working fine without "confirm" dialog. But its not working when like needs confirmation. Here is the code snippet i am using.

    private void setUpWebView() {
    likeWebView.setVerticalScrollBarEnabled(false);
    likeWebView.setHorizontalScrollBarEnabled(false);
    likeWebView.setWebViewClient(new FacebookWebViewClient());
    likeWebView.setWebChromeClient(new MyChromeClient());
    likeWebView.getSettings().setJavaScriptEnabled(true);
     String url = getFacebookLikeUrl();
    likeWebView.loadUrl(url);
 }

我也设置ViewClient和WebChromeClient。

I am also setting ViewClient and WebChromeClient.

private String getFacebookLikeUrl() {
    return "http://www.facebook.com/plugins/like.php?" + "href="
            + URLEncoder.encode(mUrl) + "&access_token="
            + URLEncoder.encode(facebook.getAccessToken());
}

请帮我解决这个问题。先谢谢了。

Please help me out to solve this issue. Thanks in advance.

推荐答案

Facebook的类似确认打开confirm_widget在新的窗口。所以,你的WebView应支持多窗口开启。这setJavaScriptCanOpenWindowsAutomatically(真)和setSupportMultipleWindows(真)为您webview-

Facebook like confirmation opens confirm_widget in new window. So your webview should support Multiple Window opening. for this setJavaScriptCanOpenWindowsAutomatically(true) and setSupportMultipleWindows(true) for your webview-

 private void setUpWebView() {
    likeWebView = new WebView(getContext());
    likeWebView.setWebViewClient(new FacebookWebViewClient());
    likeWebView.setWebChromeClient(new MyChromeClient());
    final WebSettings webSettings = likeWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);
    String url = getFacebookLikeUrl();
    likeWebView.loadUrl(url);
    likeWebView.setLayoutParams(FILL);
    mContent.addView(likeWebView);
}

Facebook的喜欢确认调用onCreateWindow()方法。 SO覆盖onCreateWindow方法WebChromeClient -

Facebook like confirmation calls onCreateWindow() method. SO override the onCreateWindow method in WebChromeClient -

final class MyChromeClient extends WebChromeClient {

    // Add new webview in same window
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog,
            boolean userGesture, Message resultMsg) {
        WebView childView = new WebView(getContext());
        childView.getSettings().setJavaScriptEnabled(true);
        childView.setWebChromeClient(this);
        childView.setWebViewClient(new FacebookWebViewClient());
        childView.setLayoutParams(FILL);
        mContent.addView(childView);
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(childView);
        resultMsg.sendToTarget();
        return true;
    }

    // remove new added webview whenever onCloseWindow gets called for new webview.
    @Override
    public void onCloseWindow(WebView window) {
        mContent.removeViewAt(mContent.getChildCount() - 1);
    }
}

confirm_widget像呼叫onCloseWindow当用户点击要么喜欢或取消。在此方法删除最后添加的WebView。

confirm_widget for like calls onCloseWindow when user click either Like or Cancel. On this method remove last added webview.

这篇关于如何处理Facebook的喜欢与确认机器人的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:13