且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过本机代码访问android浏览器的本地存储

更新时间:2022-10-16 16:22:36

似乎没有办法实现这一目标.我试图使用内容提供者访问浏览器的数据,但是您只能访问书签和历史记录.

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.

我还使用WebView而不是浏览器进行了一些测试.我尝试了两种方法:

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

  • 访问本地存储数据库

您可以访问本地存储数据库,就像普通的SQLite数据库一样.数据存储在具有两列(键和值)的单个表中.根据javascript文档,写入本地存储是同步操作.这很棒!问题在于,将数据从浏览器(webview)持久保存到文件系统是异步的.因此,如果您将某些内容写入本地存储,则它将存储在内存中,因此可以通过javascript API对其进行访问,但仍无法持久存储到SQLite数据库中.

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.

  • 通过javascript桥访问本地存储

这是我的个人推荐!您已经创建了一个Android javascript界面​​并将其添加到webview.这样,您就可以为javascript提供API来访问您的代码. 要格外小心!这可能是一个严重的安全问题!仔细检查您的代码!之后,您可以调用webview.loadUrl("javascript://...")

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