2013-07-29 22 views
0

我正嘗試使用android webview構建一個簡單的localStorage示例。我在關閉應用程序時遇到了一些麻煩。每次我啓動應用程序時,存儲在localStorage中的值都消失。我搜索了它並找不到合適的解決方案。在這裏,我把我的HTML,Java,Manifest代碼示例。任何幫助將不勝感激。當移動設備上線時,LocalStorage值消失

HTML:

<!DOCTYPE html> 
<html> 
<head> 
<script> 
function clickCounter() 
{ 
if(typeof(Storage)!=="undefined") 
    { 
    if (localStorage.clickcount) 
    { 
    localStorage.clickcount=Number(localStorage.clickcount)+1; 
    } 
    else 
    { 
    localStorage.clickcount=1; 
    } 
    document.getElementById("result").innerHTML="You have clicked the button " + localStorage.clickcount + " time(s)."; 
    } 
else 
    { 
    document.getElementById("result").innerHTML="Sorry, your browser does not support web storage..."; 
    } 
} 
</script> 
</head> 
<body> 
<p><button onclick="clickCounter()" type="button">Click me!</button></p> 
<div id="result"></div> 
<p>Click the button to see the counter increase.</p> 
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p> 
</body> 
</html> 

JAVA:

webView.getSettings().setJavaScriptEnabled(true); 
webView.getSettings().setDomStorageEnabled(true); 
webView.getSettings().setDatabaseEnabled(true); 
webView.getSettings().setDatabasePath(this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath()); 
webView.loadUrl("http://www.example.com/sample.html"); 
setContentView(webView); 

清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

權限線都放置在標籤,但標籤

這裏外面是該方案: 請關閉 - 啓動應用程序 web視圖 內的移動設備 的互聯網連接 - 打開頁面抽頭鍵數次 -close應用程序(關閉任務管理器內) 轉角在互聯網上移動設備 的連接 - 啓動應用程序 - 打開 頁面抽頭按鈕幾次,它從開始

你可以在試圖創建不同的場景計數。主要問題是如何設置webView正確使用localStorage?有些東西我對webview缺少了(當你在Chrome瀏覽器或任何其他瀏覽器上嘗試時,這個錯誤不會發生)

回答

0

這似乎就像一個webview的bug。我實際上是試圖在webview中打開3個不同的頁面,並且丟失了localStorage數據。我將這個活動分成3個活動,每個活動只打開一個頁面。問題消失了。