1. Are you ready for the Galaxy S20? Here is everything we know so far!

Android webview google login not working

Discussion in 'Android Development' started by Girish mukundan, Jul 4, 2018.

  1. Girish mukundan

    Thread Starter

    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 attached my code. please provide a solutions

    <

    package cbazaar.androidapp;

    import com.clevertap.android.sdk.CleverTapAPI;
    import com.google.android.gms.analytics.HitBuilders;
    import com.google.android.gms.analytics.Tracker;
    import android.content.DialogInterface;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.net.http.SslError;
    import android.os.Build;
    import android.os.Message;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.webkit.CookieManager;
    import android.webkit.SslErrorHandler;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.FrameLayout;
    import android.widget.ProgressBar;


    public class MainActivity extends AppCompatActivity {
    private WebView mywebView;
    private CleverTapAPI ct;
    private Tracker mTracker;
    private ProgressBar progressBar;
    private AlertDialog builder;

    private WebView mWebviewPop;
    private FrameLayout mContainer;
    private Context mContext;
    private String url = "https://www.cbazaar.com";
    private String target_url_prefix = "www.cbazaar.com";


    public String USER_AGENT_FAKE = "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19";

    @override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    try {
    GoogleanalyticsImplement application = (GoogleanalyticsImplement) getApplication();
    mTracker = application.getDefaultTracker();
    } catch (Exception e) {
    e.printStackTrace();
    }

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    mContainer = (FrameLayout) findViewById(R.id.webview_frame);
    mywebView = (WebView) findViewById(R.id.webview);
    WebSettings webSettings = mywebView.getSettings();
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    mywebView.clearHistory();
    mywebView.clearFormData();
    mywebView.clearCache(true);



    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setAppCacheEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);
    if (Build.VERSION.SDK_INT >= 21) {
    webSettings.setMixedContentMode( WebSettings.MIXED_CONTENT_ALWAYS_ALLOW );
    }

    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);


    // USER_AGENT_FAKE = mywebView.getSettings().getUserAgentString() ;

    mywebView.setWebViewClient(new MyCustomWebViewClient());
    mywebView.setWebChromeClient(new UriWebChromeClient());
    mywebView.loadUrl("https://www.cbazaar.com");
    mContext=this.getApplicationContext();

    progressBar = (ProgressBar) findViewById(R.id.progressBar_cyclic);
    mywebView.setWebViewClient(new WebViewClient() {


    @override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
    super.onPageStarted(view, url, favicon);
    progressBar.setVisibility(view.VISIBLE);
    }

    @override
    public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);
    progressBar.setVisibility(view.GONE);
    }
    }


    );
    }

    @override
    protected void onResume() {
    super.onResume();

    //sending tracking information
    mTracker.setScreenName("Main Activity");
    mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }

    @override
    public void onBackPressed() {
    if (mywebView.isFocused() && mywebView.canGoBack()) {
    mywebView.goBack();
    } else {
    super.onBackPressed();
    }
    }


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

    }

    }



    }

    >
     


    jeff1091 likes this.

    1. Download the Forums for Android™ app!


      Download

       
  2. Colbiocin

    Colbiocin Lurker

Loading...
Similar Threads - Android webview google
  1. Brincanareia
    Replies:
    0
    Views:
    31
  2. Jim1348
    Replies:
    1
    Views:
    103
  3. workbench
    Replies:
    1
    Views:
    142
  4. thkl
    Replies:
    2
    Views:
    143
  5. sadzwonek
    Replies:
    1
    Views:
    138
  6. sean5678
    Replies:
    2
    Views:
    130
  7. Philip1982
    Replies:
    0
    Views:
    843
  8. acenspades
    Replies:
    0
    Views:
    1,047
  9. cinarrtolga
    Replies:
    0
    Views:
    1,448
  10. hk1337
    Replies:
    0
    Views:
    4,715

Share This Page

Loading...