Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Reading HTML5 localStorage data direct from Java

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?

like image 496
bugfixr Avatar asked Feb 25 '12 21:02

bugfixr


1 Answers

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"));
like image 155
Savoo Avatar answered Oct 13 '22 00:10

Savoo