I am developing an android/tizen app using crosswalk project. Now i need to change the user agent to view page in desktop version. Is it possible to change user agent and how? Thank you!
Asked
Active
Viewed 3,477 times
5 Answers
5
I used reflection to solve this problem until this API goes public again 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();
}
}

cprcrack
- 17,118
- 7
- 88
- 91
-
Hey, It seems `getSettings` is no longer available. Are there any other ways of doing this? – tehmaestro Sep 27 '15 at 12:57
2
In documentation is nothing bout it. Propably only way is use setResourceClient and WebResourceResponse to change user agent. Example use:
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);
}
});

Kenumir
- 664
- 8
- 24
-
It works, but it slows down the performance and loading times. It is a very interesting answer, I've learned a lot. Thank You! – Jonson Oct 06 '14 at 16:02
-
The solution may be use this only for first url (skil for loading other resources, images etc) – Kenumir Oct 07 '14 at 07:12
1
Use this snippet:
XWalkView mXWalkView;
mXWalkView.getSettings().setUserAgentString("Your User Agent");

Uzbekjon
- 11,655
- 3
- 37
- 54

Farhad Ahmadi
- 32
- 2
- 2
-
1Please add some explanation of what your code is doing and how it works, so that newbies like the OP will be able to learn from the answer. – i alarmed alien Oct 04 '14 at 10:48
-
Didn't solve my actual problem, but the server recognizes the new user-agent. Thank You! :) – Jonson Oct 06 '14 at 16:00
1
The new API to set user agent is in draft and will be released in Crosswalk-12, about the end of January next year.
It will be like: xwalkView.setUserAgentString(newUserAgentString);

lincsoon
- 401
- 3
- 3
-
Source: https://www.mail-archive.com/crosswalk-help@lists.crosswalk-project.org/msg00634.html – cprcrack Dec 17 '14 at 19:10
0
This is for web-apps where you can alter JS-Files.:
I use this workaround, it works as well with Crosswalk 9.
xview.addJavascriptInterface(new Object(){
@JavascriptInterface
public String getNewUserAgent(){
return "My User Agent";
}
}, "NativeInterface");
And in my web-app i just call:
navigator.ua = NativeInterface.getNewUserAgent();

Ostkontentitan
- 6,930
- 5
- 53
- 71
-
Thank You. Until now I use Crosswalk 8, but that is an interesting info! – Jonson Dec 04 '14 at 11:41