如何通过本机代码访问android浏览器的本地存储 [英] How to access android browser's local storage via native code

查看:171
本文介绍了如何通过本机代码访问android浏览器的本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个必须访问默认浏览器本地存储的android应用程序.场景是:

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

  • 打开android默认浏览器
  • 加载一个页面(由我开发),该页面将一些数据保存在浏览器的本地存储中
  • 关闭浏览器
  • 打开我的应用程序
  • 应用程序读取保存在浏览器本地存储中的数据并将其显示给用户

是否可以访问浏览器的本地存储?另外,如果可能的话,如何访问设备上其他已安装浏览器的本地存储?

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?

谢谢!

推荐答案

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

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

这篇关于如何通过本机代码访问android浏览器的本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆