我正在开发一个使用人行横道项目的android/tizen应用程序。现在我需要将用户代理更改为在桌面版本中查看页面。 是否有可能更改用户代理和如何? 谢谢!是否有可能在Crosswalk Android/Tizen-Apps中更改用户代理?
回答
使用此片段:
XWalkView mXWalkView;
mXWalkView.getSettings().setUserAgentString("Your User Agent");
在文档是没有什么比它。显然,唯一的方法是使用setResourceClient和WebResourceResponse更改用户代理。 使用示例:
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);
}
});
这是Web的应用程序,你可以改变JS-文件:
我使用此解决方案,它的工作原理以及人行横道9.
xview.addJavascriptInterface(new Object(){
@JavascriptInterface
public String getNewUserAgent(){
return "My User Agent";
}
}, "NativeInterface");
而在我的网络应用程序我ju st电话:
navigator.ua = NativeInterface.getNewUserAgent();
谢谢。到现在为止,我使用Crosswalk 8,但这是一个有趣的信息! – Jonson 2014-12-04 11:41:35
设置用户代理的新API正在草拟中,并将在明年1月底的Crosswalk-12中发布。
它会像: xwalkView.setUserAgentString(newUserAgentString);
来源:https://www.mail-archive.com/[email protected]/msg00634.html – cprcrack 2014-12-17 19:10:14
我用反射来解决这个问题,直到这个API在Crosswalk 12中再次公开。这适用于9.38.208.10的Crosswalk。
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();
}
}
感谢它的工作。 – Ataru 2014-12-25 11:45:54
嘿,看起来'getSettings'不再可用。有没有其他的方法来做到这一点? – tehmaestro 2015-09-27 12:57:24
- 1. 是否有可能跟踪使用代理网站的用户?
- 2. 如何检查Crosswalk WebView用户代理?
- 3. 更改用户代理在python代理
- 4. 在iOS中是否有可供用户更改的时钟
- 5. 是否有可能更改httpservletrequest对象
- 6. 是否有可能使用WPF在c#中更改FormattedText的TextOptions?
- 7. 在php.ini中更改用户代理
- 8. 更改iPhone用户代理?
- 9. 是否可以更改npm用户?
- 10. 在Kotlin中,是否有可能在运行时更改代表团?
- 11. “用户代理”标题值是否更改过?
- 12. 是否有可能阻止用户修改HTML插件中的代码?
- 13. 是否有可能在flex中通过代理加载数据
- 14. 是否有可能在另一个进程中更改信号处理程序?
- 15. 是否有可能使用代码更改asp.net mvc web.config中的重写规则?
- 16. 是否有可能在拉取请求(Github)中更改其他人的代码?
- 17. 是否有可能在android中更改ActionBar颜色?
- 18. 是否有可能在变量中更改变量的值?
- 19. 是否有可能在asp.net中更改/限制dynamicfilter的值
- 20. 是否有可能在“while”循环中更改变量的值?
- 21. 是否有可能在运行中更改web2py视图?
- 22. 是否有可能在android中更改所选Tab的颜色?
- 23. 是否有可能在codeigniter中更改页面URL
- 24. 是否有可能在C中更改一个const struct成员?
- 25. 是否有可能在Teamcity中创建更改日志?
- 26. 是否有可能在JavaScript中更改document.activeElement?
- 27. 是否有可能在python中更改父进程的环境?
- 28. 是否有可能在
- 29. 是否有可能在代码中更新WP7锁定屏幕?
- 30. 是否有可能判断iFrame的src是否已更改?
请添加一些关于您的代码在做什么以及如何工作的解释,以便像OP这样的新手能够从回答。 – 2014-10-04 10:48:41
没有解决我的实际问题,但服务器识别新的用户代理。 谢谢! :) – Jonson 2014-10-06 16:00:51
从此人行横道9将不再工作。 – Ostkontentitan 2014-12-03 09:57:31