最近接了一个私活,就是给H5应用套Android的壳子,用webview加载h5,会访问的时候是需要用户登录的,为了防止用户一直登录的问题,找了很多资料,用来保存webview的cooker
setCooker
//Android 5.0及以上版本使用WebView不能存储第三方Cookies解决方案
Android 5.0以及以上版本使用webview是不能存储第三方的cookies的,所以我们需要自己存
public void setCookies(String cookiesPath) { CookieSyncManager.createInstance(this); Map cookieMap = new HashMap<>(); String cookie = getSharedPreferences("cookie", Context.MODE_PRIVATE).getString("cookies", "");// 从SharedPreferences中获取整个Cookie串 Log.i("cookie", cookie); if (!TextUtils.isEmpty(cookie) && !cookie.equals("null")) { String[] cookieArray = cookie.split(";");// 多个Cookie是使用分号分隔的 for (int i = 0; i < cookieArray.length; i++) { int position = cookieArray[i].indexOf("=");// 在Cookie中键值使用等号分隔 String cookieName = cookieArray[i].substring(0, position);// 获取键 String cookieValue = cookieArray[i].substring(position + 1);// 获取值 String value = cookieName + "=" + cookieValue;// 键值对拼接成 value Log.i("cookie", value); CookieManager.getInstance().removeSessionCookie();// 移除 CookieManager.getInstance().setCookie(getDomain(cookiesPath), value);// 设置 Cookie// CookieManager.getInstance().setAcceptCookie(true);// CookieSyncManager.getInstance().sync(); } } }
public void setCookies(String cookiesPath) {
CookieSyncManager.createInstance(this);
CookieManager.getInstance().removeSessionCookie();// 移除
CookieManager.getInstance().setAcceptCookie(true);
Map cookieMap =new HashMap<>();
String cookie = getSharedPreferences("cookie", Context.MODE_PRIVATE).getString("cookies","");// 从SharedPreferences中获取整个Cookie串
Log.i("cookie", cookie);
if (!TextUtils.isEmpty(cookie) && !cookie.equals("null")) {
String[] cookieArray = cookie.split(";");// 多个Cookie是使用分号分隔的
for (int i =0; i < cookieArray.length; i++) {
int position = cookieArray[i].indexOf("=");// 在Cookie中键值使用等号分隔
String cookieName = cookieArray[i].substring(0, position);// 获取键
String cookieValue = cookieArray[i].substring(position +1);// 获取值
String value = cookieName +"=" + cookieValue;// 键值对拼接成 value
Log.i("cookie", value);
CookieManager.getInstance().setCookie(getDomain(cookiesPath), value);// 设置 Cookie
}
}
CookieManager.getInstance().setCookie(getDomain(cookiesPath),"Domain=domain");
CookieManager.getInstance().setCookie(getDomain(cookiesPath),"Path=/");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().flush();
}else {
CookieSyncManager.getInstance().sync();
}
}
//退出页面的时候保存cookie,不仅要在onDestory中写还要写再onPause中,点击home键的时候会走onPause的生命周期
@Override
protected void onDestroy() {
super.onDestroy();
CookieManager cookieManager = CookieManager.getInstance();
StringBuilder sbCookie =new StringBuilder();
String cookieStr = cookieManager.getCookie(getDomain(urlTest));
if (!TextUtils.isEmpty(cookieStr) && !cookieStr.equals("null")){
sbCookie.append(cookieStr);
SharedPreferences preferences = getSharedPreferences("cookie", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("cookies", sbCookie.toString());
editor.commit();
}
}
@Override
protected void onPause(){
super.onPause();
CookieManager cookieManager = CookieManager.getInstance();
StringBuilder sbCookie =new StringBuilder();
String cookieStr = cookieManager.getCookie(getDomain(urlTest));
if (!TextUtils.isEmpty(cookieStr) && !cookieStr.equals("null")){
sbCookie.append(cookieStr);
SharedPreferences preferences = getSharedPreferences("cookie", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("cookies", sbCookie.toString());
editor.commit();
}
}
//退出的时候用到的getDomain获取域名的方法
/**
* 获取URL的域名
*/
private String getDomain(String url) {
url = url.replace("http://","").replace("https://","");
if (url.contains("/")) {
url = url.substring(0, url.indexOf('/'));
}
return url;
}
这样运行以后就可以了
问题:第一,必须和后台同时联调。domain为域名,path为 /根目录, 或者是用户可以访问的根目录
第二,路径指定不能指定到登录界面(我们的后台给的就是个指定到登录的界面的地址,结果搞了两天也没有搞成功)
第三,注意 cooker更新的问题
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().flush();
}else {
CookieSyncManager.getInstance().sync();
}