9

I've got a simple Android app that has a WebView. The WebView is set to browse to a site which uses JavaScript's localStorage feature.

I've already got my WebSettings set to allow DomStorage:

webSettings.setJavaScriptEnabled(true);
ebSettings.setDomStorageEnabled(true);

String dbPath = this.getApplicationContext().getDir("database", MODE_PRIVATE).getPath();        
webSettings.setDatabasePath(dbPath);

What I need is a way that my Java code can read a variable stored using the localStorage mechanism, ie.:

The JavaScript does this:

    var storage = window.localStorage;
    storage.setItem("name", "Hello World!");

How can I read the value of "name" from localStorage from Java code?

1
  • Not a single solution here works for me. Did you have any luck? Commented Jan 21, 2020 at 19:57

4 Answers 4

2

yes possible to read localStorage value in java (Android).

use this plugin https://www.npmjs.com/package/cordova-plugin-nativestorage that use native storage .

-for that we have to set value in cordova

    NativeStorage.setItem("reference", obj, setSuccess, setError);
    function setSuccess(obj){
    }
    function setError(obj){
    }

And In Anroid Java File to get this value :

    SharedPreferences sharedPreferences = getSharedPreferences("MainActivity", MODE_PRIVATE);
    System.out.println("********---------    shared pref values...   " +  sharedPreferences.getString("myid", "no value"));
Sign up to request clarification or add additional context in comments.

Comments

0
yourWebView.setWebChromeClient(new WebChromeClient(){
      public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        Log.d(tag, "That's my local storage value =" + message);
        return false;
      };
    });
(...)
    yourWebView.loadURL("javascript:alert(localStorage.getItem(\"name\"))");

2 Comments

This gives me: "Uncaught SecurityError: Failed to read the 'localStorage' property from 'Window': Access is denied for this document."
@Onheiron I am also facing same issue. Did you solved it?
0

in your activity

webView.addJavascriptInterface(myInterface, "JSInterface");

Javascript interface class

class myInterface
{

    @JavascriptInterface
    public void getVariable(string fromLocalStorage)
    {
    //your code
    }

}

in javascript

window.JSInterface.getVariable(localStorage.getItem("variableName"))

1 Comment

This utilizes JavaScript for getting the value. I need my Android application to request the value from the browser, not the browser to initiate it. I can't call webView.loadUrl or webView.evaluateJavaScript using this method as it never actually does it.
0

To write data :

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 +"');");
}

To read and alert data :

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
     webView.evaluateJavascript("window.alert(localStorage.getItem('"+ key +"'));", null);
} else {
     webView.loadUrl("javascript:window.alert(localStorage.getItem('"+ key +"'));");
}

And remember to enable JavaScript of Android WebvView

webView.getSettings().setJavaScriptEnabled(true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.