5

Here with the code in Android Studio. I also add the html code that below the Android Studio code, using javascript to remember the username and password in cookies. It works in some android device only. I don'y know why. I would like to make it works for all the android device. Hope it will be a simple way to do it. Please kindly help. I want the user typed in his username/password one time, it should be filled out on next visit.

public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;


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


        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Go To Our Facebook Page");

        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://www.hksalonjob.comslogin.php");

    }

here with the html code, using javascript to remember the username and password.

    <!DOCTYPE HTML>

    <html>
        <head>
            <title>Salon Job HK</title>
            <meta http-equiv="content-type" content="text/html; charset=utf-8" />
            <meta name="description" content="" />
            <meta name="keywords" content="" />
            <!--[if lte IE 8]><script src="css/ie/html5shiv.js"></script><![endif]-->
            <script src="js/jquery.min.js"></script>
            <script src="js/jquery.scrolly.min.js"></script>
            <script src="js/jquery.dropotron.min.js"></script>
            <script src="js/jquery.scrollex.min.js"></script>
            <script src="js/skel.min.js"></script>
            <script src="js/skel-layers.min.js"></script>
            <script src="js/init.js"></script>
            <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
            <noscript>
                <link rel="stylesheet" href="css/skel.css" />
                <link rel="stylesheet" href="css/style.css" />
                <link rel="stylesheet" href="css/style-xlarge.css" />
            </noscript>
            <!--[if lte IE 9]><link rel="stylesheet" href="css/ie/v9.css" /><![endif]-->
            <!--[if lte IE 8]><link rel="stylesheet" href="css/ie/v8.css" /><![endif]-->

        </head>
        <body>

            <!-- Header -->


            <!-- Main -->



        <div id="main" class="wrapper style1">
        <div class="container">


          <form ACTION="<?php echo $loginFormAction; ?>" name="form1" method="POST">



                                        <div class="12u$">
                                          <input type="tel" name="tel" required id="tel"  pattern="[0-9]{1}[0-9]{7}"  maxlength="8" value="" placeholder="phone number"/>
                                        </div>
                                        <br>

                                        <div class="12u$">
                                          <input type="password" name="spassword" required id="spassword"  maxlength="8" placeholder="" />
                                        </div>

                                        <br>
                                        <div class="12u$">
                                        <input type="checkbox" name="sremember" id="sremember" />
                                        <label for="sremember">Remember me</label>

                                        </div>


                          <INPUT Type="submit" class="button special fit" name="Login" id="Login" value="login">

            </form>  


            <script>
            //remember username and password
                        $(function() {

                            if (localStorage.chkbx && localStorage.chkbx != '') {
                                $('#sremember').attr('checked', 'checked');
                                $('#tel').val(localStorage.tel);
                                $('#spassword').val(localStorage.spassword);
                            } else {
                                $('#sremember').removeAttr('checked');
                                $('#tel').val('');
                                $('#spassword').val('');
                            }

                            $('#sremember').click(function() {

                                if ($('#sremember').is(':checked')) {
                                    // save username and password
                                    localStorage.tel = $('#tel').val();
                                    localStorage.spassword = $('#spassword').val();
                                    localStorage.chkbx = $('#sremember').val();
                                } else {
                                    localStorage.tel = '';
                                    localStorage.spassword = '';
                                    localStorage.chkbx = '';
                                }
                            });
                        });

            </script>

        </div>
        </div>

    </body>
    </html>
A.D.
  • 1,412
  • 2
  • 19
  • 37
Eric Chong
  • 495
  • 1
  • 7
  • 21

1 Answers1

5

Please check whether the webview stores cookies, e.g. like this:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Go To Our Facebook Page");

    //not sure if you need this
    CookieSyncManager.createInstance(this;
    //it is default true, but hey... 
    CookieManager.getInstance().setAcceptCookie(true);

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {

        //Do you have cookies?
        Log.d("Cookie", "url: " + url + ", cookies: " + CookieManager.getInstance().getCookie(url)); 
    }

    });
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.hksalonjob.comslogin.php");

}

EDIT: Ok, forget my lines above :-)

Be careful, what i write here is no good practice in security perception. I only show you the basics how it could basically work (totally untested).

You can exchange data between Java and Javascript with JavascriptInterface:

In Java:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("Go To Our Facebook Page");


    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient() {

    @Override
    public void onPageFinished(WebView view, String url) {

        //Is the url the login-page?
        if (url.equals ("http://www.hksalonjob.comslogin.php") == true) {

             //load javascript to set the values to input fields
             SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
             String usr= prefs.getString("usr", null);
             String pwd= prefs.getString("pwd", null);
             if (usr== null || pwd == null) {
                 //we  have no values - leave input fields blank
                 return;
             }
             view.loadUrl("javascript:fillValues(" + usr + "," + pwd + ");");
        } 
    }

    });

    myWebView.addJavascriptInterface(new JavaScriptInterface(), "Android");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("http://www.hksalonjob.comslogin.php");

}


private class JavaScriptInterface {

    /**
    * this should be triggered when user and pwd is correct, maybe after
    * successful login
    */
    public void saveValues (String usr, String pwd) {

      if (usr == null || pwd == null) {
           return;
      }

      //save the values in SharedPrefs
      SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
      editor.putString("usr", usr);
      editor.putString("pwd", pwd);
      editor.apply();
    } 
}

In Javascript:

//trigger this after successful login
function afterSuccessLogin(usr, pwd) {

    if (Android != undefined) {
        if (Android.saveValues != undefined) {

            Android.saveValues(usr, pwd);  
        }
    }       
}
// this will be triggered by webview 
function fillValues(usr, pwd) {

    //fill input fields with values       
}
A.D.
  • 1,412
  • 2
  • 19
  • 37
  • Thank you for your help. I us your code and test it by virtual machine Genymotion (Google Nexus 5) and REAL DEVICE (LG G3) it doesn't work. But the original code from me is working for SAMSUNG S3. There must be some problems. Do you have any idea? – Eric Chong Aug 04 '15 at 07:14
  • @Eric: Please, please, please...we need some log information about "it doesn't work" – A.D. Aug 04 '15 at 07:20
  • Sorry! I am new, is it what you need? – Eric Chong Aug 04 '15 at 07:35
  • 08-04 03:35:03.037 21248-21248/com.gph.hksalonjob I/chromium﹕ [INFO:CONSOLE(100)] "Uncaught TypeError: Cannot read property 'chkbx' of null", source: http://www.hksalonjob.com/slogin.php (100) 08-04 03:35:03.038 21248-21248/com.gph.hksalonjob D/Cookie﹕ url: http://www.hksalonjob.com/slogin.php, cookies: PHPSESSID=8deb2fe2566aaa3c1b89febf61f4d7e8 – Eric Chong Aug 04 '15 at 07:37
  • 08-04 03:35:09.679 21248-21294/com.gph.hksalonjob W/EGL_emulation﹕ eglSurfaceAttrib not implemented 08-04 03:35:09.679 21248-21294/com.gph.hksalonjob W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xabbd4340, error=EGL_SUCCESS 08-04 03:35:17.440 21248-21248/com.gph.hksalonjob D/Cookie﹕ url: http://www.hksalonjob.com/stylist.php, cookies: PHPSESSID=f13d78beec3639e1b10f8dfebafae433 08-04 03:35:20.418 21248-21248/com.gph.hksalonjob I/chromium﹕ [INFO:CONSOLE(64)] "The "fb-root" div has not been created, auto-creating", source: http://connect.facebook.net/en_US/sdk.js (64) – Eric Chong Aug 04 '15 at 07:38
  • 08-04 03:35:20.420 21248-21248/com.gph.hksalonjob D/Cookie﹕ url: http://www.hksalonjob.com/index.php, cookies: PHPSESSID=f13d78beec3639e1b10f8dfebafae433 08-04 03:35:22.676 21248-21248/com.gph.hksalonjob I/chromium﹕ [INFO:CONSOLE(100)] "Uncaught TypeError: Cannot read property 'chkbx' of null", source: http://www.hksalonjob.com/slogin.php (100) 08-04 03:35:22.784 21248-21248/com.gph.hksalonjob D/Cookie﹕ url: http://www.hksalonjob.com/slogin.php, cookies: PHPSESSID=f13d78beec3639e1b10f8dfebafae433 – Eric Chong Aug 04 '15 at 07:38
  • @Eric: Ok, maybe i did not get your goal. If the user typed in his username/password one time, it should be filled out on next visit. Is this your goal? – A.D. Aug 04 '15 at 08:19
  • YES, this is what I want. – Eric Chong Aug 04 '15 at 08:30
  • I want the user typed in his username/password one time, it should be filled out on next visit. – Eric Chong Aug 04 '15 at 08:47
  • the app go to the 1st page www.hksalonjob.com, there are2 login page for different kind of user: 1) "www.hksalonjob.com/login.php" 2) "www.hksalonjob.com/slogin.php" – Eric Chong Aug 04 '15 at 09:36
  • android studio find error in the code myWebView.addJavascriptInterface(new JavaScriptInterface(), "Android"); – Eric Chong Aug 04 '15 at 10:29
  • can't complie, there is error " .addJavascriptInterface" before compile – Eric Chong Aug 04 '15 at 13:30