How to achieve multitasking for webviews in Android?

I want to create an application that allows users to register multiple accounts on the same site using a different webview.

For example, I have 2 WebViews.
Each WebView will load the same site as gmail.com.
And the user can login using a separate account in a separate WebView.

But the problem the one I'm running into is that 2 WebViews are always logged into the same account .

I've googled a lot and here are some related titles,
Facebook MultiLogin in Android Browser Android

Using WebView to Multiple Site Login and Fetch Data
Multiple Accounts on Separate Web Windows? (Android)
but no acceptable answer found.

Is this possible in Android using WebView?
How can I achieve what I want?

+3


source to share


2 answers


The tricky part is android.webkit.CookieManager used by the WebView to store cookies is for single player play. This means that there will only be one CookieManager instance in a Java / Dalvik process, and your multiple WebView instances within the same process have the same set of cookies.

Like @ToYonos you can try overriding some hooks to get around this, but I don't think it will work 100% ... Also think about android.webkit.WebStorage: this is another singleton!

However, this might work somewhat more reliably: duplicate your top-level WebView activity in the manifest and assign them to run in different processes:



<activity
    android:name=".WebViewActivity" />
<activity
    android:name=".WebView1Activity"
    android:process=":web1" />
<activity
    android:name=".WebView2Activity"
    android:process=":web2" />
...

      

So, you will have isolated processes and different CookieManager / WebStorage instances.

But beware: different instances of WebStorage are still written to the same path in the application data folder! You can work around this by calling webView.getSettings (). SetDatabasePath () for different paths db for different processes, but this API is deprecated in the API 19 (KitKat). Ok, as long as the webpage you are visiting is not using HTML5 local storage this should be fine ...

+5


source


I think you will have to implement your own system. You can try something like this:

private static final String DOMAIN = "http://cookiedomain.com";

private final Map<WebView, String> cookiesMap = new HashMap<WebView, String>();

// [...]

WebView w = new WebView(this);
// Loading url and stuff
w.setWebViewClient(new WebViewClient()
{
    public void onLoadResource (WebView view, String url)
    {
        // If cookies have already been stored for this WebView
        if (cookiesMap.get(view) != null)
        {
            CookieManager.getInstance().removeAllCookie();
            CookieManager.getInstance().setCookie(DOMAIN, cookiesMap.get(view));
        }
    }

    public void onPageFinished(WebView view, String url)
    {
        // Check if the url matches the after-login page or whatever you want
        boolean condition = ...;  
        if(condition)
        {
            // Getting new cookies
            String cookies = CookieManager.getInstance().getCookie(DOMAIN);
            cookiesMap.put(view, cookies);
        }
    }
});

// Do the same for the 2nd WebView

      

This is a simple example that needs to be improved, but it can be a good start for a sustainable solution.



Limitations:

  • It will only work if each WebView is not making a request at the same time as the others. Otherwise, it will most likely obfuscate the cookies.
  • This will only work for one domain
+3


source







All Articles