【问题标题】:Android web view local storage does not refresh on android 4.3 or less OSAndroid 网络视图本地存储在 android 4.3 或更低版本的操作系统上不刷新
【发布时间】:2013-12-26 15:05:15
【问题描述】:

对于 android webview 应用程序,动态 webview 页面(页面 A)在页面 A 首次加载时正确地从本地存储加载项目。然后,在页面 B 上添加了另一个本地存储项。然后,使用 window.location.herf= "page A" 加载页面 A。但是,页面 A 没有加载页面 B 上添加的额外本地存储项。似乎页面 A 总是在第一次加载后从缓存中加载。在任何 android 4.3 或更低版本的操作系统上运行的任何 android 设备或模拟器都会出现此问题。

注意:************* 页面 A 在 android 4.4 或 chrome web 上运行的模拟器上正确刷新。


所以...我想要一个适用于任何 4.3 或更低版本的 Android OS 的解决方法。

动态填充页面A内容的html.js函数

function ShowResults(value) {

     var overNight = getNetWork(value);  /// it the function to get local storage based on value key
  document.write('<li><a href="file:///android_asset /SettingEditProgram.html?overnightkey='+value+'">')
  document.write('<div class="column">')
  document.write('<div class="rectangle"><network>'+ overNight.network + '</network></br>')
  document.write('<program>'+ overNight.program +'</program></br>')
  document.write('<text>' + overNight.start +'-'+ overNight.end + '</text></br>')
  document.write('<text>' + overNight.demo + ', ' + overNight.region + '</text>')

  document.write('</div>')
  document.write('</div></a></li>')

    }

页面 A:_________________________

<head>
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='-1'>
<meta http-equiv='pragma' content='no-cache'>
<meta http-equiv="refresh" content="4">
<link rel="stylesheet"  href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />  
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<link rel="stylesheet" href="file:///android_asset/style.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>
<script src="html.js"></script>

<script>
$(document).ready(function() {
    $.ajaxSetup({ cache: false});


    var parameters = $(location).attr('search');
    parameter = parameters.replace("?overnightkey=",""); 
    // 'Setting' data-attributes using setAttribute
    var newKey = document.getElementById("overnight");
    var oldValue = newKey.getAttribute("data-overnight",newKey);
    newKey.setAttribute("data-overnight",newKey); 
    var newKeyvalue ="oldValue="+oldValue+" newValue:"+newKey; 
    console.log("newKeyvalue");

});

</script>



</head> 
<body> 

<div data-role="page" id="album-list">

<div id="headerBar">
<a href="file:///android_asset/MyShowsHomePage.html"><img class="goback"     src="file:///android_asset/img/ic_goback-web.png"></a>
<img class="appicon" src="file:///android_asset/img/ic_launcher-web.png"><p   class="apptitle">Program Setting</p>
<a href="file:///android_asset/SettingAddProgram.html"><img class="add"  src="file:///android_asset/img/ic_add-web.png"></a>
</div>

<div data-role="content">

 <ul id="overnight" data-role="listview" data-split-icon="gear" data-split-theme="d" data-overnight="0">

     <script>

     getIDs().forEach(ShowResults);

    </script>

 </ul>

</div><!-- /content -->
</div><!-- /page -->


</body>
</html>

设置本地存储然后重定向到页面A的页面B:


$('#savebutton').click(function() {

        var keyOverNight = localStorage.getItem("keyOverNightAll");

        var keyOverNightArray = new Array();

        if (keyOverNight) {
            try {
                keyOverNightArray = JSON.parse(keyOverNight); // use JSON3 to handle  multiple browsers/versions
            } catch (e) {
                //keyOverNightArray = new Array(); // for parse failure --> return an empty Array
                // do nothing
            }
        }


        //Get a new key for setting page based on the local storage
        //OvernightKeys
        var newKey;
        if (keyOverNightArray.length == 0)
        {
            newKey =1;
        }
        else
        {
            newKey= keyOverNightArray[keyOverNightArray.length-1]+1;
        }

        //Store the overnight key
        keyOverNightArray.push(newKey);

        localStorage.setItem("keyOverNightAll",JSON.stringify(keyOverNightArray));

        //Now store Program values into the newKey
        var overnightKey = "overNight"+ newKey.toString();

        var jsonOvernight = {"network": NetworkSelected, "program": ProgramSelected, "start": STimeSelected, "end": ETimeSelected, "region": RegionSelected, "demo": DemoSelected }
        localStorage.setItem(overnightKey, JSON.stringify(jsonOvernight));

        //window.location = "file:///android_asset/SettingHomePage.html";
        window.location.href = 'file:///android_asset/SettingHomePage.html?newKey='+newKey;
        //window.location.reload(true);

        });

我在页面 A 上尝试过的事情 每 10 秒刷新一次。它不会从本地存储中读取最新的元素。 将缓存设置为无内容 在PageB上添加项目后,每次进入此页面时设置一个新的数据属性 欺骗页面 A 加载带有时间戳的 html.js 脚本。例如。 html.js?'+timeStamp+'somestring:" 重新加载页面 A。然后它会无限运行。

如果我关闭应用程序并重新运行它,它将仅在第一次加载时再次加载最新的本地存储项。

我已经尝试了几乎大部分堆栈溢出帮助.. 还没有找到任何答案。

【问题讨论】:

  • getIDs() 获取 json 本地存储的所有密钥。然后,getNetWork(value) 使用这些键以 json 格式检索最终的本地存储值。

标签: android eclipse webview local-storage


【解决方案1】:

查看 targetSdkVersion。 我遇到同样的问题。我将 targetSdkVesrion 设置为低于 16。然后 sloved。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多