[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