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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM