当我的应用程序启动时(在onResume()内),我运行一个类似于以下的逻辑:
if ( appHasBeenIdleFor30Minutes() ) { CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeSessionCookie(); CookieSyncManager.getInstance().sync(); }
这会正确地重置从用户上一个会话中设置的任何会话cookie.我的问题是:这种行为是否会定期发生?这个问题(android webview or browser not deleting session cookies on device reboot)似乎暗示它没有.当我通过服务使用cookie-sync’d HttpClient时,似乎会话cookie不会被清除,从而导致奇怪的服务器端行为.
我一直无法在WebView / CookieSyncManager中找到关于会话cookie生命周期(到期时间= 0)的具体文档 – 还有其他人有更多的运气吗?
解决方法
@H_403_13@ 我直接收到了Google工程师的回复,他确认了我的怀疑:You are correct,session cookies do not expire automatically in the
lifecycle of a WebView.
If you are seeing issues with this,you can always clear all of your
cookies or overwrite your session cookies explicitly with an empty value.The code you have suggested looks like a good workaround,just be aware
that cookie synchronisation using a CookieSyncManager is not synchronous –
the startSync(),stopSync() and sync() commands are executed
asynchronously in a background thread.
TL; DR – 会话cookie在WebView关闭时不会过期,您必须自己管理它.