[android] 让原声android嵌入H5 支持localStorage

最近在做一款嵌入原生Android H5游戏.写完在Run app后发现报错,错误指向的是localStorage.setItem

 

     
    
    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啦......

posted @ 2016-12-29 15:07  DemonGao  阅读(2423)  评论(0编辑  收藏  举报