Using local storage in an Android web browser

I am experimenting with Android code: I would like to save one value using local HTML 5 storage. For this exercise, I use the page as simple as this one: http://www.w3schools.com/html5/tryit.asp?filename= tryhtml5_webstorage_local_clickcount

My manifest allows me to go online, and this is min-sdk of 7.

Here is my Java code:

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); webview = (WebView) findViewById(R.id.webview); WebView webView = (WebView) findViewById(R.id.webview); webView.addJavascriptInterface(new JavaScriptInterface(this), "Android"); WebSettings webSettings = webview.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDatabasePath(""); webSettings.setDomStorageEnabled(true); webview.setWebChromeClient(new WebChromeClient()); webview.loadUrl("http://www.xyz.com/test.html"); ///xyz.com/test.html is a sample :) webview.setWebViewClient(new HelloWebViewClient()); } 

My problem is that when I close the application, the locally stored value no longer exists. I can go to the same page using the default browser, and the value remains constant even after the emulator is closed, which is exactly the behavior I'm looking for.

This is probably something extremely simple ... any ideas?

+3
source share
3 answers

An empty DatabasePath string appears. I tried the same code and with an empty string path, the value is not saved after the application exits. If I define a specific database path, the value is saved as expected.

Try:

 webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/"); 
+7
source

If your application uses multiple webviews, you will still have problems: localStorage is not correctly distributed across all webviews.

If you want to share the same data in multiple web views, the only way is to restore it using the java database and javascript interface.

This github page shows how to do this.

hope this help!

+2
source

It was not possible to make it work on all devices (especially with ICS) - even using a database path that allows DOMStorage, etc. - using cookies instead helped me.

+1
source

All Articles