1. Source
Recently, Google login requires that Google Play Service be followed on the mobile phone. Some mobile phones, such as Xiaomi, do not use Google Play Servcie. Therefore, it is necessary to implement a webview login method. Google redirects are configured by the Server. We only need to request a URL to jump to the Google authorization page. After authorization, the Server jumps to the Server where the Server obtains user information, then, return the user information to an address. You only need to parse the returned page results.
2. Code Implementation
Permission<Uses-permission android: name = "android. permission. INTERNET"/>
Clear cookieTry {CookieSyncManager. createInstance (this); CookieManager. getInstance (). removeAllCookie ();} catch (Exception e) {Log. d ("Google Login", "Clear cookie failed ");}
Final Page DetectionWebView webview = (WebView) findViewById (R. id. login); webview. setWebViewClient (new WebViewClient () {@ Override public void onPageFinished (WebView view, String url) {if (url. contains (succeedUrl) Log. d ("Google Login", "onPageFinished, url =" + url) ;}}); webview. loadUrl (requestUrl );
To obtain the page content, you must use JS to call JAVA code.Class Handler {@ JavascriptInterface // This is required for api level17. If this parameter is not added, no error is reported, that is, no call is required. It took one and a half hours to complete. Public void show (String data) {Log. d ("Google Login", "handler, show, data =" + data); System. out. println ("data =" + data) ;}} webview. getSettings (). setJavaScriptEnabled (true); // sets the webview available for JS. addJavascriptInterface (new Handler (), "handler"); // register the Handler object and name it handler public void onPageFinished (WebView view, String url) {if (url. contains (succeedUrl) Log. d ("Google Login", "onPageFinished, url =" + url); view. loadUrl ("javascript: window. handler. show (document. body. innerHTML );");}});
3. ReferencesClear cookie http://developer.android.com/reference/android/webkit/CookieSyncManager.html how to get the content http://veikr.com/201106/android_webview_content-html.html in webView in Android