安卓:网络视图
我正在尝试使用 shouldOverrideUrlLoading 将 web 应用程序(例如 www.xyz.com)包装在 web 视图中。我有两个启动应用程序的活动,它检查所有正在运行的活动并确定是否启动新实例以及将我的应用程序 www.xyz.com 包装在 Web 视图中的其他活动。
我面临两个问题:
当我按下睡眠按钮然后再次按下它打开设备时,或者当应用程序空闲并进入睡眠状态并且我按下按钮打开设备时,应用程序会重新启动( webview 重新启动)。
此外,当打开其他网站的网络应用程序中的链接(例如 www.abc.com)无法正常工作时,它第一次可以正常工作并根据需要在外部浏览器中打开该网站,但是进一步单击会尝试打开Web 视图中的网站是不需要的。
我已经用谷歌搜索了这个并找到了如下代码的建议:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("abc")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
但是,就我而言,第一次在外部浏览器中打开网站 www.abc.com,但第二次在 webview 中打开它,我已经在其中调试了它eclipse 并发现后续 cliks 中的 url 值相同。
I am trying to wrap a web application say www.xyz.com within a webview using shouldOverrideUrlLoading. I have two activity one that launches the application, this checks all the activity running and determines whether to launch new instance or not and other activity which wraps my application www.xyz.com in the webview.
I'm facing two problems:
When I press the sleep button and then again press it to open the device, or when the application is idle and went to sleep and I press the button to open the device, the application restarts (the webview restarts).
Also when link in the webapplication which opens someother site say for example www.abc.com doesn't work fine, it works for the first time and opens the site in external browser as desired, however on further click tries to open the site within the webview which is not desired.
I have googled this and found suggestion like the code below:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("abc")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
However, in my case for the first time the site www.abc.com is opened in external browser but for the second time its open within the webview, i've debugged it in eclipse and found the url value comes the same in subsequent cliks .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)对于WebView重启问题,您需要将以下几行代码添加到包含WebView的Activity中。
另请参阅有关如何在Bundle中保存WebView状态的教程。
2)对于您的外部浏览器问题,请尝试
而不是
希望能解决您的问题:)
1) For the WebView restart problem, you need to add the following lines of code to your activity that contains the WebView
Also see tutorials on how to save state of WebView in Bundle.
2) For your external browser problem, try
instead of
Hope that solves your problem :)