js交互android
AngerlarJsweb
/angular-cookies/angular-cookies.min.js 下文件改成/angular-cookies/angular-cookies.js裏面的內容 而後將json
this.$get = ['$$cookieReader', '$$cookieWriter', function($$cookieReader, $$cookieWriter) {
//下面重寫get put remove方法。cookie
get: function(key) { var value = JsGKey.getMessage(key); return value; // return $$cookieReader()[key]; },
put: function(key, value, options) { JsPutKey.getMessage(key,value); // $$cookieWriter(key, value, calcOptions(options)); },
remove: function(key, options) { JsRemoveKey.getMessage(key); // $$cookieWriter(key, undefined, calcOptions(options)); }
在android 下面dom
webview.addJavascriptInterface(new TmActivity.JsGKey(), "JsGKey"); webview.addJavascriptInterface(new TmActivity.JsPutKey(), "JsPutKey"); webview.addJavascriptInterface(new TmActivity.JsRemoveKey(), "JsRemoveKey");
private class JsGKey { @JavascriptInterface public String getMessage(String key) { String cookie = CookieManager.getInstance().getCookie(domain); JSONObject jsonObject = null; String value = null; if (cookie!=null) { try { jsonObject = new JSONObject(cookie); value = jsonObject.getString(key); } } catch (JSONException e) { e.printStackTrace(); } } return value; } }
public class JsPutKey { @JavascriptInterface public void getMessage(String key, String value) { String cookie = CookieManager.getInstance().getCookie(domain); JSONObject jsonObject = null; String result; if (cookie == null) { try { jsonObject = new JSONObject(); result= String.valueOf(jsonObject.put(key,value)); CookieManager.getInstance().setCookie(domain, result); } catch (JSONException e) { e.printStackTrace(); } } else { try { jsonObject = new JSONObject(cookie); jsonObject.put(key, value); CookieManager.getInstance().setCookie(domain, String.valueOf(jsonObject)); String ck = CookieManager.getInstance().getCookie(domain); } catch (JSONException e) { e.printStackTrace(); } } } }
public class JsRemoveKey { @JavascriptInterface public void getMessage(String key) { //刪除目前沒有想好應對的辦法 String cookie = CookieManager.getInstance().getCookie(domain); JSONObject jsonObject = null; if(cookie == null){ return; }else{ try { jsonObject = new JSONObject(cookie); jsonObject.remove(key); CookieManager.getInstance().setCookie(domain, String.valueOf(jsonObject)); } catch (JSONException e) { e.printStackTrace(); } } } }
若是你們轉帖請標記好原創地址https://my.oschina.net/u/3407708/blog。this