Can I change the user agent in Crosswalk Android / Tizen-Apps?

I am developing android / tizen application using crosswalk project. Now I need to change the user agent to view the page in the desktop version. Can the user agent be changed and how? Thank!

+3


source to share


5 answers


Use this snippet:



XWalkView mXWalkView;
mXWalkView.getSettings().setUserAgentString("Your User Agent");

      

+1


source


I used reflection to solve this problem until this API becomes public in Crosswalk 12 . This works in Crosswalk 9.38.208.10.



private void setWebViewUserAgent(XWalkView webView, String userAgent)
{
    try
    {
        Method ___getBridge = XWalkView.class.getDeclaredMethod("getBridge");
        ___getBridge.setAccessible(true);
        XWalkViewBridge xWalkViewBridge = null;
        xWalkViewBridge = (XWalkViewBridge)___getBridge.invoke(webView);
        XWalkSettings xWalkSettings = xWalkViewBridge.getSettings();
        xWalkSettings.setUserAgentString(userAgent);
    }
    catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
    {
        // Could not set user agent
        e.printStackTrace();
    }
}

      

+5


source


There is nothing in the documentation. The only way to use it is using setResourceClient and WebResourceResponse to change the user agent. Usage example:

setResourceClient(new XWalkResourceClient(this) {
    @Override
    public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) {
        try {
            URL u = new URL(url);
            HttpURLConnection c = (HttpURLConnection) u.openConnection(Proxy.NO_PROXY);
            c.setRequestProperty("User-agent", "test user agent");
            return new WebResourceResponse(null, "utf-8", c.getInputStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.shouldInterceptLoadRequest(view, url);
    }
});

      

+3


source


The new API for installing the user agent is in draft and will be released in Crosswalk-12 around the end of January next year.

It will look like this: xwalkView.setUserAgentString (newUserAgentString);

+1


source


This is for web apps where you can modify JS files .:

I am using this workaround, it also works with Crosswalk 9.

xview.addJavascriptInterface(new Object(){
            @JavascriptInterface
            public String getNewUserAgent(){
                return "My User Agent";
            }
        }, "NativeInterface");

      

And in my web application, I just call:

navigator.ua = NativeInterface.getNewUserAgent();

      

0


source







All Articles