How to save a cookie in an Android webview forever?

后端 未结 3 1761
悲哀的现实
悲哀的现实 2020-12-02 19:06

With my code below, I have been able to save a cookie, but as soon as I close the application the cookie disappears.

How is this caused and how can I solve it?

3条回答
  •  醉话见心
    2020-12-02 20:04

    For those who are facing Problems with CookieManager class to make cookie persist even after closing the app, they should try the flush() function of CookieManager.

    Please note that i haven't tried this, so if it works then please let me know too.

    According to android documentation

    void flush()

    Ensures all cookies currently accessible through the getCookie API are written to persistent storage. This call will block the caller until it is done and may perform I/O.

    Also in CookieSyncManager documnentation it is written that:

    This class was deprecated in API level 21. The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager. To manually force a sync you can use the CookieManager method flush() which is a synchronous replacement for sync(). CookieSyncManger link

提交回复
热议问题