Android的WebView中localStorage的 [英] Android WebView localStorage

查看:311
本文介绍了Android的WebView中localStorage的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个HTML5的localStorage例如一个Android的WebView(WebKit的,ChromeClient)内工作。但我有没有运气。一切从手机浏览器的工作方式不是从web视图。

活动:

 公共类BrowserActivity延伸活动{
    @覆盖
    公众最终无效的onCreate(最终捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);        的WebView浏览器=(的WebView)findViewById(R.id.webView);        browser.setHapticFeedbackEnabled(真);
        browser.getSettings()setJavaScriptEnabled(真)。
        。browser.getSettings()setSavePassword(假);        browser.getSettings()setDatabaseEnabled(真)。
        browser.getSettings()setDomStorageEnabled(真)。
        browser.getSettings()setDatabasePath(/数据/数据​​/+ browser.getContext()getPackageName()+/数据库/)。        browser.loadUrl(http://my.app.com:10099);
    }
}

Manifiest:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
          包=RestrictedBrowser
          安卓版code =1
          机器人:=的versionName1.0>
    <采用-SDK安卓的minSdkVersion =16/>
    <应用机器人:标签=@字符串/ APP_NAME>
        <活动机器人:名字=RestrictedBrowser.BrowserActivity
                  机器人:标签=@字符串/ APP_NAME
                  机器人:主题=@安卓风格/ Theme.Black.NoTitleBar
                  机器人:screenOrientation =肖像>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>
    <使用许可权的android:NAME =android.permission.INTERNET对>< /使用许可权>
    <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E>< /使用许可权>
    <使用许可权的android:NAME =android.permission.READ_EXTERNAL_STORAG​​E>< /使用许可权>
< /清单>

错误


  

WARN / System.err的(3961):
  android.database.sqlite.SQLiteConstraintException:列软件包名
  是不是唯一的(code 19)07-12 13:57:43.425:WARN / System.err的(3961):在
  android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(母语
  法)07-12 13:57:43.425:WARN / System.err的(3961):在
  android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:857)
  WARN / System.err的(3961):在
  android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
  WARN / System.err的(3961):在
  android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
  WARN / System.err的(3961):在
  android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665)
  WARN / System.err的(3961):在
  android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
  ERROR / SQLiteLog(28735):(14)在无法打开文件
  的[00bb9c9ce4]行30241 07-12 13:57:47.005:ERROR / SQLiteLog(28735):
  (14)os_unix.c:30241:(2)开(/NotificationPermissions.db) - 07-12
  DEBUG / WebKit的(28735):SQLite数据库无法从加载
  /NotificationPermissions.db
          原因 - 无法打开数据库文件07-12 13:57:47.005:DEBUG / WebKit的(28735):28735:28758 D / WebKit的]
          外部/ WebKit的/来源/ WebCore的/平台/ SQL / SQLiteDatabase.cpp(71):
  布尔的WebCore :: SQLiteDatabase ::开放(常量WTF ::串放;,布尔)



解决方案

您必须创建数据库目录,然后才能使用它。

使用以下code

 字符串databasePath = browser.getContext()。GETDIR(数据库
                                   Context.MODE_PRIVATE).getPath();
browser.getSettings()setDatabasePath(databasePath)。

I'm attempting to get a HTML5 localStorage example working within an Android WebView (Webkit, ChromeClient). However I'm having no luck. Everything works from the phones browsers just not from the WebView.

Activity:

public class BrowserActivity extends Activity {
    @Override
    public final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        WebView browser = (WebView) findViewById(R.id.webView);

        browser.setHapticFeedbackEnabled(true);        
        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setSavePassword(false);

        browser.getSettings().setDatabaseEnabled(true);
        browser.getSettings().setDomStorageEnabled(true);
        browser.getSettings().setDatabasePath("/data/data/" + browser.getContext().getPackageName() + "/databases/");

        browser.loadUrl("http://my.app.com:10099");
    }
}

Manifiest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="RestrictedBrowser"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16"/>
    <application android:label="@string/app_name">
        <activity android:name="RestrictedBrowser.BrowserActivity"
                  android:label="@string/app_name"
                  android:theme="@android:style/Theme.Black.NoTitleBar"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
</manifest>

Error

WARN/System.err(3961): android.database.sqlite.SQLiteConstraintException: column packagename is not unique (code 19) 07-12 13:57:43.425: WARN/System.err(3961): at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method) 07-12 13:57:43.425: WARN/System.err(3961): at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:857) WARN/System.err(3961): at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754) WARN/System.err(3961): at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64) WARN/System.err(3961): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1665) WARN/System.err(3961): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594) ERROR/SQLiteLog(28735): (14) cannot open file at line 30241 of [00bb9c9ce4] 07-12 13:57:47.005: ERROR/SQLiteLog(28735): (14) os_unix.c:30241: (2) open(/NotificationPermissions.db) - 07-12 DEBUG/WebKit(28735): SQLite database failed to load from /NotificationPermissions.db Cause - unable to open database file 07-12 13:57:47.005: DEBUG/WebKit(28735): [ 28735:28758 D/WebKit ] external/webkit/Source/WebCore/platform/sql/SQLiteDatabase.cpp(71) : bool WebCore::SQLiteDatabase::open(const WTF::String&, bool)

解决方案

You must create the database directory before you can use it.

Use the following code

String databasePath = browser.getContext().getDir("databases", 
                                   Context.MODE_PRIVATE).getPath(); 
browser.getSettings().setDatabasePath(databasePath); 

这篇关于Android的WebView中localStorage的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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