如何在android webview中像确认一样处理facebook

发布于 2024-12-05 09:53:18 字数 829 浏览 0 评论 0原文

我正在尝试使用 android webview 实现类似 facebook 的功能。没有“确认”对话框,它工作正常。但当需要确认时它不起作用。这是我正在使用的代码片段。

    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。

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

请帮我解决这个问题。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2024-12-12 09:53:18

Facebook 之类的确认会在新窗口中打开confirm_widget。所以你的 webview 应该支持多窗口打开。对于此 setJavaScriptCanOpenWindowsAutomatically(true) 和 setSupportMultipleWindows(true) ,您的 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() 方法。 请重写 WebChromeClient 中的 onCreateWindow 方法 -

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

因此,当用户单击 Like 或 Cancel 时, recognize_widget 来调用 onCloseWindow。在此方法中删除最后添加的 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 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.

羅雙樹 2024-12-12 09:53:18

我将其用于 Stripe Checkout,它在移动设备中打开一个新窗口进行支付。

基于@Shweta的回应:

在您的活动中:

package myapp.app;
/*** imports ***/

public class LoggedActivity extends FragmentActivity
{
    public WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logged);

        // retrieve the main container
        LinearLayout container = (LinearLayout) findViewById(R.id.logged_webviews_container);

        // layout params applied to the webviews in order to fit 100% the parent container
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);

        myWebView = new WebView(this);
        myWebView.setLayoutParams(layoutParams);
        myWebView.setWebViewClient(new BetterWebViewClient(this));

        WebSettings settings = myWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setSupportMultipleWindows(true);

        // on this instruction, we set our extended class below as the Parent Webview webchromeclient
        myWebView.setWebChromeClient(new PopupWebView(this, myWebView, container, layoutParams));

        // load URL
        myWebView.loadUrl('http://www.mywebsite.com');
        container.addView(myWebView);
    }

}

添加扩展WebChromeClient的类

package myapp.app;
/*** imports ***/

public class PopupWebChromeClient extends WebChromeClient {

    protected Activity activity;
    protected WebView parentWebView;
    protected RelativeLayout container;
    protected WebView popupView;

    PopupWebChromeClient(
        Activity activity,
        WebView parentWebView,
        RelativeLayout container
    )
    {
        super();
        this.activity = activity;
        this.parentWebView = parentWebView;
        this.container = container;
    }

    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {

        this.parentWebView.setVisibility(WebView.GONE);

        this.popupView = new WebView(this.activity);

        // setup popuview and add
        this.popupView.getSettings().setJavaScriptEnabled(true);
        this.popupView.setWebChromeClient(this);
        this.popupView.setWebViewClient(new ApkfWebViewClient(this.activity, true));
        this.popupView.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT
        ));
        this.container.addView(this.popupView);

        // send popup window infos back to main (cross-document messaging)
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(popupView);
        resultMsg.sendToTarget();

        return true;
     }

    // remove new added webview on close
    @Override
    public void onCloseWindow(WebView window) {
        this.popupView.setVisibility(WebView.GONE);
        this.parentWebView.setVisibility(WebView.VISIBLE);
    }


}

在您的布局xml中,不要设置webview,因为我们是动态创建它们的。

I used this for Stripe Checkout which opens a new window in mobile devices for payments.

Based on @Shweta's response :

In your activity:

package myapp.app;
/*** imports ***/

public class LoggedActivity extends FragmentActivity
{
    public WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logged);

        // retrieve the main container
        LinearLayout container = (LinearLayout) findViewById(R.id.logged_webviews_container);

        // layout params applied to the webviews in order to fit 100% the parent container
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);

        myWebView = new WebView(this);
        myWebView.setLayoutParams(layoutParams);
        myWebView.setWebViewClient(new BetterWebViewClient(this));

        WebSettings settings = myWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setSupportMultipleWindows(true);

        // on this instruction, we set our extended class below as the Parent Webview webchromeclient
        myWebView.setWebChromeClient(new PopupWebView(this, myWebView, container, layoutParams));

        // load URL
        myWebView.loadUrl('http://www.mywebsite.com');
        container.addView(myWebView);
    }

}

Add this class which extends WebChromeClient

package myapp.app;
/*** imports ***/

public class PopupWebChromeClient extends WebChromeClient {

    protected Activity activity;
    protected WebView parentWebView;
    protected RelativeLayout container;
    protected WebView popupView;

    PopupWebChromeClient(
        Activity activity,
        WebView parentWebView,
        RelativeLayout container
    )
    {
        super();
        this.activity = activity;
        this.parentWebView = parentWebView;
        this.container = container;
    }

    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {

        this.parentWebView.setVisibility(WebView.GONE);

        this.popupView = new WebView(this.activity);

        // setup popuview and add
        this.popupView.getSettings().setJavaScriptEnabled(true);
        this.popupView.setWebChromeClient(this);
        this.popupView.setWebViewClient(new ApkfWebViewClient(this.activity, true));
        this.popupView.setLayoutParams(new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT
        ));
        this.container.addView(this.popupView);

        // send popup window infos back to main (cross-document messaging)
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(popupView);
        resultMsg.sendToTarget();

        return true;
     }

    // remove new added webview on close
    @Override
    public void onCloseWindow(WebView window) {
        this.popupView.setVisibility(WebView.GONE);
        this.parentWebView.setVisibility(WebView.VISIBLE);
    }


}

In your layout xml, don't set webviews since we create them on the fly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文