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

时光总嘲笑我的痴心妄想 提交于 2019-12-30 09:48:36

问题


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!


回答1:


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://...")




回答2:


I used SharedPreferences Plugin to access the native storage instead of accessing the browser local storage inside Phonegap. This might be useful for some. The Github Code



来源:https://stackoverflow.com/questions/15804714/how-to-access-android-browsers-local-storage-via-native-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!