2011-12-05 96 views
15

使用我的代碼,我可以保存一個cookie,但是一旦關閉應用程序,cookie就會消失。如何將cookie永久保存在Android Web瀏覽器中?

這是如何造成的,我該如何解決?

package com.jkjljkj 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Window; 
import android.webkit.CookieSyncManager; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Toast; 

public class Activity extends Activity { 

    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     CookieSyncManager.createInstance(getBaseContext()); 

     // Let's display the progress in the activity title bar, like the 
     // browser app does. 


     getWindow().requestFeature(Window.FEATURE_PROGRESS); 

     WebView webview = new WebView(this); 
     setContentView(webview); 


     webview.getSettings().setJavaScriptEnabled(true); 

     final Activity activity = this; 
     webview.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
      // Activities and WebViews measure progress with different scales. 
      // The progress meter will automatically disappear when we reach 100% 
      activity.setProgress(progress * 1000); 
     } 
     }); 

     webview.setWebViewClient(new WebViewClient() { 

     public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       //Users will be notified in case there's an error (i.e. no internet connection) 
       Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
     } 
     }); 

     CookieSyncManager.getInstance().startSync(); 
     CookieSyncManager.getInstance().sync(); 

    //This will load the webpage that we want to see 
     webview.loadUrl("http://"); 

    } 
} 
+0

CookieSyncManager現在已被棄用,使用CookieManager。 http:// stackoverflow。com/questions/30502411/cookiesyncmanager-is-now-deprecated-what-c​​an-use-instead –

+0

@Rahul Sahni我嘗試過,但如果關閉應用程序並重新打開,我必須重新登錄,所有cookies丟失!你能告訴我可能是什麼問題嗎? – user1788736

回答

35

你必須告訴CookieSyncManager同步後已加載頁面的問題。在示例代碼中,onCreate方法在WebView嘗試加載頁面之前完全執行,因此在加載頁面之前可能會完成同步過程(異步發生)。

而是告訴CookieSyncManager在WebViewClient中同步onPageFinished。這應該讓你得到你想要的。

CookieSyncManager Documentation是一個很好的解讀如何正確地做到這一點。

下面是如何,你可以設置你的WebViewClient實施爲你這樣做:

webview.setWebViewClient(new WebViewClient() { 
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     //Users will be notified in case there's an error (i.e. no internet connection) 
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
    } 

    public void onPageFinished(WebView view, String url) { 
     CookieSyncManager.getInstance().sync(); 
    } 
); 

你不會需要告訴CookieSyncManager到位與此同步的其他地方。我沒有測試過,所以讓我知道它是否有效。

+0

我是全新的......你能給我一些實際的代碼嗎?這將有助於一噸! – Joe

+1

你走了。給它一個旋轉。 – lyricsboy

+2

你搖滾!這很好,純粹的天才!多謝,夥計! – Joe

4

.sync()強制執行同步操作,並且必須在頁面加載後調用它使RAM與緩存同步,因此在調用它之前,Cookie必須在ram中。如果你使用這個方案

onCreate: 
CookieSyncManager.createInstance(context) 

onResume: 
CookieSyncManager.getInstance().startSync() 

onPause: 
CookieSyncManager.getInstance().stopSync() 

我想你沒有等待5分鐘路程,從而節省系統餅乾

系統自動同步它每隔5分鐘路程。

+0

我該怎麼辦如果我想立即同步cookie,而無需在android 6上等5分鐘? – user1788736

0

對於那些面臨CookieManager類問題的人來說,即使在關閉應用程序後cookie仍然存在,他們應該嘗試使用的功能CookieManager

請注意,我還沒有嘗試過,所以如果它的作品,請讓我知道。

根據Android文檔

void flush()

確保目前通過的getCookie API訪問的所有Cookie被寫入到永久存儲。這個調用將阻止調用者,直到它完成並且可以執行I/O。

此外,在CookieSyncManager documnentation它寫的是:

這個類是在API級別21 不推薦使用的WebView現在會自動同步必要的餅乾。您不再需要創建或使用CookieSyncManager。要手動強制同步,您可以使用CookieManager方法flush(),它是sync()的同步替換。 CookieSyncManger link

+0

@ user1788736希望這可以幫助你 –