Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access android browser's local storage via native code

I am developing an android application that has to access the default browser local storage. The scenario is :

  • Open the android default browser
  • Load a page (that I have developed) that saves some data in the browser's local storage
  • Close the browser
  • Open my application
  • The application reads the data saved in the browser's local storage and shows it to the user

Is there a way to access the local storage of the browser? Also if it is possible, how to access the local storage of the other installed browser on the device?

Thanks!

like image 771
Kiril Aleksandrov Avatar asked Oct 05 '22 12:10

Kiril Aleksandrov


1 Answers

It seems that there is no way to achieve this. I have tried to access browser's data using content providers but you can access only bookmarks and history.

I have also made some tests using WebView instead of browser. I tried two approaches:

  • Access the local storage database

You can access the local storage database like normal SQLite database. The data is stored in a single table with two columns - key and value. According to the javascript documentation writing to the local storage is a synchronous operation. This is great! The problem is that persisting the data from the browser(webview) to the file system is asynchronous. So if you write something to the local storage it is stored in the memory so it is accessible through javascript API but it is still NOT persisted to the SQLite database.

  • Access the local storage via javascript bridge

This is my personal recommendation! You have create an Android javascript interface and add it to the webview. This way you provide an API to the javascript to access your code. Be extremely careful! This could be a serious security issue! Double check your code! After that you can call your javascript methods like this webview.loadUrl("javascript://...")

like image 199
Kiril Aleksandrov Avatar answered Oct 10 '22 03:10

Kiril Aleksandrov