In KitKat and above, this code works perfectly:
ActivityManager manager = ((ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE));
manager.clearApplicationUserData();
It wipes all of the app settings, any internal databases, webview caches, etc. It's great.
I want to write some code that will do this same thing for users with versions below KitKat.
I've tried:
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear com.example.myapp");
That doesn't work.
Also tried:
CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
That doesn't work.
Also tried a bunch of clearCache(), clearHistory(), .setAppCacheEnabled(false), etc on the webview. No luck.
Basically, my biggest problem is the webview seems to still be caching data (ie. if I've logged into a site through the webview, I want to be able to delete that session permanently and log the user out). Nothing I do (except clearApplicationUserData) is able to fix this.
Thanks
If anyone else has this problem I did this to fix it:
CookieSyncManager syncManager = CookieSyncManager.createInstance(activity);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
syncManager.stopSync();
syncManager.sync();
Related
Lets prepare for cookie storing:
CookieSyncManager.createInstance(getApplicationContext());
CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().setAcceptCookie(true);
Then I'm putting manually some cookies, lets say PHPSESSID and RANDOM
CookieManager.getInstance().setCookie("domain.com", "PHPSESSID="+phpSession);
CookieManager.getInstance().setCookie("domain.com", "RANDOM="+random);
lets check is it working using:
CookieManager.getInstance().getCookie("domain.com");
and got
PHPSESSID=dba4ff392agd39b5951d10a91a0a7b56; RANDOM=266284790:1466147978:c91d0896bac59e0b
Everything looks good, but when I navigate in may app to one of WebView Activities, which are opening same domain website also setting cookies, then when I print cookie like above it looks like this:
PHPSESSID=dba4ff392agd39b5951d10a91a0a7b56;
RANDOM=266284790:1466147978:c91d0896bac59e0b;
PHPSESSID=9ecb5156cf8fc3190fbc69fd13393243;
RANDOM=265078219%3A1463147975%3Ad0448d163e9b2123
duplicated entries... when after that I manually set again e.g. RANDOM with setCookie:
PHPSESSID=dba4ff392agd39b5951d10a91a0a7b56;
RANDOM=111111111:2222222222:33333336bac59e0b;
PHPSESSID=9ecb5156cf8fc3190fbc69fd13393243;
RANDOM=265078219%3A1463147975%3Ad0448d163e9b2123
values set by WebView are not overwritten, only my "manually" entered... how to force WebView to use my earlier set cookie OR overwrite already set?
As in the MDN docs about Set-Cookie you can see many different kinds of cookie values, a cookie can be set to particular Path
cookie-name=cookie-value; Path=path-value
And in CookieManager.setCookie void setCookie (String url, String value), the android reference says:
Sets a cookie for the given URL. Any existing cookie with the same host, path and name will be replaced with the new cookie. The cookie being set will be ignored if it is expired.
In my opinion, the reason that you have duplicate entries is because the cookie values were in different Path. So if you want to overwrite you should make sure host path name are the same.
private void setCookieManager(String auth_token) {
CookieSyncManager manager = CookieSyncManager.createInstance(this);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie(auth_token);
manager.sync();
}
I think you just didn't setup your cookie manager properly. Give this a try
first of all, can someone please explain how does CookieManager.getInstance() work? I don't really get how I can get the session from webview? Lets say if I have this
CookieSyncManager.createInstance(WebviewPage.this);
CookieManager cookieManager = CookieManager.getInstance();
Do I get the session from the class named WebviewPage? but what if I named my actual WebView to webview, how can cookieManager get the session of webview? not even talk about if I had two WebView, webview1 and webview2. How do I know which session that was stored in cookieManager??
My main question is...I have two activities and one webview in each activity. How can I get the session from Activity A and pass it to the webview in Activity B?
Thanks!!!
As far as I know, you don't need to set cookie for webview2.
webview2 will automatically use the cookies from webview1.
CookieManager seems to be a singleton, so when you call getInstance() you always get the same instance. So if webview 1 set some cookies on the CookieManager, or if you set it your self using set Cookie, all other webviews should get the same cookies as well.
How do I enable cookies in a webview?
I tried to use
CookieManager.getInstance().setAcceptCookie(true);
just before calling WebView.loadUrl() and it doesn't work as I get an HTML page error from a website saying cookies need to be enabled.
How does cookieManager know which webview to enable cookies?
Say if I had an activity with two webviews in the screen and I only wanted one of those webviews to enable cookies, how is that possible using a CookieManager?
I feel like I am missing something. I could not find a method like webView.setCookieManager or Cookiemanager.setWebView(webview).
You should consider that
CookieManager.getInstance().setAcceptCookie(true);
doesn't work from lollipop(API21) and above. You should check and use appropriate function for that case:
if (android.os.Build.VERSION.SDK_INT >= 21) {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
} else {
CookieManager.getInstance().setAcceptCookie(true);
}
CookieManager.getInstance() is the CookieManager instance for your entire application.
Hence, you enable or disable cookies for all the webviews in your application.
Normally it should work if your webview is already initialized:
http://developer.android.com/reference/android/webkit/CookieManager.html#getInstance()
Maybe you call CookieManager.getInstance().setAcceptCookie(true); before you initialize your webview and this is the problem?
I'm trying to set some cookies on my WebView to open a browser with the same session that I have on my app.
I read a lot of answers but they don't work for me. The only solution I've found is in the loadUrl, hardcode the cookie data in extraHeaders, but as expected this only works for this requests, and doesn't maintain the session.
The code that I have is:
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();
cookieManager.setCookie("http://xx.xxx.example.com","mid="+MySession.GetSession().sessionId+" ; Domain=.example.com");
cookieSyncManager.sync();
String cookie = cookieManager.getCookie("http://xx.xxx.example.com");
Log.d(LOGTAG, "cookie ------>"+cookie);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new TuWebViewClient());
mWebView.loadUrl("http://xx.xx.example.com");
getCookie() returns the correct data, but when I read the cookies from the server, those are empty. What is wrong? Please advise.
Thank you!!!
Solved!!!! the problem is with the webView, I dont know what happend, but If I create the
WebView webView = new WebView(Activity.this);
it works. If I read the webview from activity with findViewById() it doesn't work.
Also if you need to set a list of cookies that you received previously from a website.
All you have to do is use a for-loop to go through and set all of them . It helped me to solve the situation
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(mWebView.getContext());
...
cookieSyncManager.sync();
is the cause of problem. You should do like this:
CookieSyncManager.createInstance(mWebView.getContext());
...
CookieSyncManager.getInstance().sync();
And there will be no need to manually create WebView...
I'm using a WebView in an Android app. I periodically to be able to reset the WebView http session so that the server app it is accessing will initialise a new session. I can't find how to do this - is this possible ?
Rgds, Kevin.
I think the clear cookie can make session close. More about session
CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
import android.webkit.CookieManager;
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeSessionCookie();
removeSessionCookie() allows to keep other cookies but clear sessions.
There are a couple of things you can do to clear the webview depending exactly what you want to do:
webView.clearCache(true);
webView.clearHistory();
webView.destroy();
This worked for me:
WebStorage.getInstance().deleteAllData()