2011-11-19 138 views
4

我試圖讓android代碼的這個WebView保持會話cookie,以便當應用程序關閉並且用戶再次啓動它時,他們可以保持登錄狀態。我非常新,並且我越來越沮喪,試圖弄清楚這一點。我需要改變什麼?請善待我,因爲我是一個白癡,請描述性,即使你必須爲我編輯它,如果這對你更容易。保存Cookie Webview?

我導入了CookieManager和CookieSyncManager,但沒有IDEA接下來要做什麼。我已閱讀所有的文檔,並花了幾個小時試圖找出如何使它保存Cookie,這樣用戶就不必再次登錄,我實在不明白它...

package com.template.WebViewTemplate; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.webkit.CookieManager; 
import android.webkit.CookieSyncManager; 

public class WebViewTemplate extends Activity { 
private WebView myWebView; 
/** Called when the activity is first created. */ 
@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) { // Enables browsing to previous pages with the hardware back button 
     myWebView.goBack(); 
     return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    myWebView = (WebView) findViewById(R.id.webview); // Create an instance of WebView and set it to the layout component created with id webview in main.xml 
    myWebView.getSettings().setJavaScriptEnabled(true); 
    myWebView.loadUrl("http://zuneboards.com/forums/mgc_chatbox.php?do=view_chatbox"); // Specify the URL to load when the application starts 
    //myWebView.loadUrl("file://sdcard/"); // Specify a local file to load when the application starts. Will only load file types WebView supports 
    myWebView.setWebViewClient(new WebViewKeep()); 
    myWebView.setInitialScale(1); // Set the initial zoom scale 
    myWebView.getSettings().setBuiltInZoomControls(true); // Initialize zoom controls for your WebView component 
    myWebView.getSettings().setUseWideViewPort(true); // Initializes double-tap zoom control 

} 

private class WebViewKeep extends WebViewClient { 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return true; 
    } 

} }

+0

CookiesManager這裏描述[此帖] [1]Android文檔 [1]:http://stackoverflow.com/questions/2566485/webview-and-cookies-on-android –

回答

5

您可以Websetting使用此:

webView.getSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);