[android] 讓原聲android嵌入H5 支持localStorage

最近在作一款嵌入原生Android H5遊戲.寫完在Run app後發現報錯,錯誤指向的是localStorage.setItemweb

 

     
    
    WebView browser=(WebView)findViewById(R.id.Toweb);
    /***打開本地緩存提供JS調用**/
        browser.getSettings().setDomStorageEnabled(true);
        // Set cache size to 8 mb by default. should be more than enough
        browser.getSettings().setAppCacheMaxSize(1024*1024*8);

        // This next one is crazy. It's the DEFAULT location for your app's cache
        // But it didn't work for me without this line.
        // UPDATE: no hardcoded path. Thanks to Kevin Hawkins
        String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
        browser.getSettings().setAppCachePath(appCachePath);
        browser.getSettings().setAllowFileAccess(true);
        browser.getSettings().setAppCacheEnabled(true);

     //優先加載緩存頁
     browser.getSettings().setCacheMode( browser.getSettings().LOAD_CACHE_ELSE_NETWORK);

這樣 H5頁面就支持localStorage啦......緩存

相關文章
相關標籤/搜索