Can I change the user agent in Crosswalk Android / Tizen-Apps?
5 answers
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 to share
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 to share
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 to share