Saturday, May 21, 2011

WebView - Part 4 Handling Cookies in WebView


Handling Cookies in WebView

Actually this is very simple, as we mentioned in Part 1 of this series, that for security reasons, your application will have its own cache and its own cookie store i.e. it doesn’t share the Browser’s application data, cookies are also managed in separate thread which means that those operations which are for managing cookies (e.g. building index) will not block your application’s UI thread.

Now, we use “android.webkit.CookieSyncManager” class to manage cookies particularly to synchronize the browser cookies between RAM and permanent storage. To get maximum efficiency cookies are stored in RAM and a separate thread is used to save cookies this thread is driven by timer, sync interval is of 5 Minutes.

To use CookieSyncManager all you have to do is add these three lines in your code
Call this when the application starts.
 
CookieSyncManager.createInstance(context);
 
To set up sync call in Activity.onResume()
 
CookieSyncManager.getInstance().startSync();
 
and lastly, call this in Activity.onPause()
 
CookieSyncManager.getInstance().stopSync();
 
To get sync instantly without waiting for the timer to trigger, host can call
 
CookieSyncManager.getInstance().sync();
 
But be aware that this sync also happens asynchronously, so don’t do it just as your activity is shutting down.

<<WebView - Part 3 How to customize WebView?




Cheers!!
Vivek Jain
Hello Everyone, I came across an application which allowed me to produce nice effects on Images that are in my phone and Images that I take from my phones camera, its available here, 

1 comment: