Android Google登录无法在WebView中使用 [英] Android Google login not working inside WebView

查看:208
本文介绍了Android Google登录无法在WebView中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手.尝试在Android网络视图中集成FB和Google+登录.FB登录正常.但是Google登录名不允许登录.我提到了一些链接,但无法成功.

I am new to android development. Trying to integrate FB and Google+ login in Android web view. FB login is working fine. But Google login is not allowing to login. I referred a few links but unable to succeed.

问题是在Gmail中提供了用户名和密码后,我的网站未登录

Problem is after providing user name and password in Gmail my web site is not sign in

覆盖另一个Web视图的Web视图

Google登录无法正常运行的android webview应用程序

Google登录无法正常运行的android webview应用程序

private class MyCustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        String host = Uri.parse(url).getHost();
        Log.d("Loading URL", url);
        if (host.equals(target_url_prefix)) {
            // This is my web site, so do not override; let my WebView load
            // the page
            if (mWebviewPop != null) {
                mWebviewPop.setVisibility(View.GONE);
                mContainer.removeView(mWebviewPop);
                mWebviewPop = null;
            }
            return false;
        }

        if (host.contains("m.facebook.com") || host.contains("facebook.co")
                || host.contains("google.co")
                || host.contains("www.facebook.com")
                || host.contains(".google.com")
                || host.contains(".google")
                || host.contains("accounts.google.com/signin/oauth/consent")
                || host.contains("accounts.youtube.com")
                || host.contains("accounts.google.com")
                || host.contains("accounts.google.co.in")
                || host.contains("www.accounts.google.com")
                || host.contains("oauth.googleusercontent.com")
                || host.contains("content.googleapis.com")
                || host.contains("ssl.gstatic.com")
            //     || host.contains("https://accounts.google.com/signin/oauth/consent")

        ) {
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch
        // another Activity that handles URLs

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        Log.d("onReceivedSslError", "onReceivedSslError");
        super.onReceivedSslError(view, handler, error);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        if (url.startsWith("https://m.facebook.com/v2.7/dialog/oauth")


        ) {
            if (mWebviewPop != null) {
                mWebviewPop.setVisibility(View.GONE);
                mContainer.removeView(mWebviewPop);
                mWebviewPop = null;
            }
            view.loadUrl("https://www.cbazaar.com");
            return;
        }

        super.onPageFinished(view, url);
    }
}

private class UriWebChromeClient extends WebChromeClient {

    @Override
    public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
        mWebviewPop = new WebView(mContext);
        mWebviewPop.setVerticalScrollBarEnabled(false);
        mWebviewPop.setHorizontalScrollBarEnabled(false);
        mWebviewPop.setWebViewClient(new MyCustomWebViewClient());
        mWebviewPop.setWebChromeClient(new UriWebChromeClient());
        mWebviewPop.getSettings().setJavaScriptEnabled(true);
        mWebviewPop.clearHistory();
        mWebviewPop.clearFormData();
        mWebviewPop.clearCache(true);
        mWebviewPop.getSettings().setSavePassword(true);
        mWebviewPop.getSettings().setSaveFormData(true);
        mWebviewPop.getSettings().setUserAgentString(USER_AGENT_FAKE);

        builder = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).create();

        builder.setTitle("");
        builder.setView(mWebviewPop);

        builder.setButton("close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                mWebviewPop.destroy();
                dialog.dismiss();

            }
        });

        builder.show();
        builder.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

             /*   CookieManager cookieManager = CookieManager.getInstance();
                cookieManager.setAcceptCookie(true);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    cookieManager.setAcceptThirdPartyCookies(mWebviewPop,true);
                }
    */

        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(mWebviewPop);
        resultMsg.sendToTarget();

        return true;
    }


    @Override
    public void onCloseWindow(WebView window) {

        try {
            mWebviewPop.destroy();
        } catch (Exception e) {
        }

        try {
            builder.dismiss();

        } catch (Exception e) {
        }

    }

}

推荐答案

Google不允许使用WebView的默认实现.因此,您需要为WebView设置自定义User-Agent:

Google does not allow default implementations of WebView to be used. Therefore you need to set a custom User-Agent to your WebView:

webView.getSettings().setUserAgentString("YourAppName");

您可以使用任何字符串代替 YourAppName .

You can use any string instead of YourAppName.

这篇关于Android Google登录无法在WebView中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆