14

I have a webview android application which opens a web page containing some HTML5/ JavaScript code. I want to pass some values from the android side of my application to the browser side. So I thought to write onto HTML5 localstorage from Android and then the Javascript part of the web page reads the value from the localstorage.

How can Android webview write onto the HTML5 localstorage?

Or is there at all a way the android can pass some values to javascript of the page it loads? ( without having to reload the entire page) Say writing something onto HTML5 localstorage and then the javascript code reads that thing from HTML5 localstorage

its different from how to pass json formatted data from a webview to a html page. I need a way of writing onto HTML5 localstorage by android

Waterfr Villa
  • 1,217
  • 1
  • 10
  • 33
  • Possible duplicate of [How to pass JSON-formatted data from a WebView to a HTML page](http://stackoverflow.com/questions/10114993/how-to-pass-json-formatted-data-from-a-webview-to-a-html-page) – petey May 18 '17 at 17:43
  • Why is it necessary for you to pass data to a webview? –  Jun 27 '17 at 06:13
  • Have you tried using `setDomStorageEnabled`? – Pedro Oliveira May 10 '19 at 10:19

3 Answers3

22

Pass variables like a string stackoverflow post:

   webView.getSettings().setJavaScriptEnabled(true);
   webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
               String key = "hello";
               String val = "world";
               if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                   webView.evaluateJavascript("localStorage.setItem('"+ key +"','"+ val +"');", null);
               } else {
                   webView.loadUrl("javascript:localStorage.setItem('"+ key +"','"+ val +"');");
               }
            }
   });

The second variant is to use JavaScriptInterface

Init section

 JavaScriptInterface jsInterface = new JavaScriptInterface(this);
 webView.getSettings().setJavaScriptEnabled(true);
 webView.addJavascriptInterface(jsInterface, "JSInterface");

JavaScriptInterface

  public class JavaScriptInterface {
        private Activity activity;
    
        public JavaScriptInterface(Activity activiy) {
            this.activity = activiy;
        }
    
        public string getData(String someParameter){
           //also you can return json data as string  and at client side do JSON.parse
           if (someParameter == "give me data" && this.activity.data != null) {
                return this.activity.data;
           }else{
                return null;
           }
        }
    }

Js section

<script>
  function ready() {
        var data = window.JSInterface.getData("give me data");
        localStorage.put("give me data", data)
  };

  document.addEventListener("DOMContentLoaded", ready);
</script>
Alex Nikulin
  • 8,194
  • 4
  • 35
  • 37
  • Thank you for your answer. Helped me a lot. – André Luiz Reis Jul 25 '18 at 20:00
  • How to get `JSInterface` if website is in angular (typescript)? – KSK Mar 17 '20 at 06:58
  • I am getting below exception java.lang.RuntimeException: Stub! at android.content.Context.(Context.java:4) at android.content.ContextWrapper.(ContextWrapper.java:5) at android.view.ContextThemeWrapper.(ContextThemeWrapper.java:5) at android.app.Activity.(Activity.java:6) at main.java.tools.MyWebView.(MyWebView.java:8) – Baala Apr 08 '20 at 12:11
  • @AndréLuizReis - Can you please share the code snippet. Currently looking to pass local storage value for appium test – Baala Apr 08 '20 at 12:12
  • @Alex Nikulin hellooo, could you please help me at https://stackoverflow.com/questions/63498385/how-to-pass-parameters-in-webview similar with this – pratik vekariya Aug 25 '20 at 03:53
2

How can Android webview write onto the HTML5 localstorage?

   localStorage.setItem("data", value);

Note: With local storage, web applications can store data locally within the user's browser. HTML local storage; better than cookies.
Try addJavaScriptInterface() to bind your android class and webpage.

paulcab
  • 1,102
  • 1
  • 9
  • 16
-3

You can do it by Location Hash Property of javascript..

Android Code may look like this

WebView myWebView = (WebView) findViewById(R.id.myWebView);  
myWebView.loadUrl("http://www.example.com/#any_value");
myWebView.setWebViewClient(new MyWebViewClient());

And Javascript code look like this

var x = location.hash; //this will access the hash value you pass in url
localStorage.setItem("save_my_value", x);
Mehtab
  • 453
  • 5
  • 17