Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

How to handle facebook like with confirm in android webview

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);
 }

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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 for like calls onCloseWindow when user click either Like or Cancel. On this method remove last added webview.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...