Android:单击网页视图中页面中的链接
我在android web view中包含了一个web应用程序,并且网页中有一个链接可以打开其他网站,当单击该链接时,第一次单击可以正常工作,但是当第二次单击时,找不到该网站,
代码是:
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some site ")) {
Intent i = new
Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
@THelper和@mikegr,感谢您的回复,
实际上就我而言,我的网络应用程序中有一个模式面板(JSF),其中包含一些按钮,单击按钮后我将使用javascript窗口打开其他一些网站。打开()在桌面浏览器中工作正常的方法,但是,当我将此 Web 应用程序包装在 android webview 中时,一切正常,除了当我第一次单击此按钮时,我可以使用外部浏览器打开其他网站,但是在第二次单击 webview 时尝试在 web 视图中而不是外部浏览器中打开此其他网站,但我找不到带有其他网站的完整 URL 的网站,即使我注销并再次登录,因为启动的应用程序仍在运行,也会发生这种情况。
在我的情况下,当应用程序空闲一段时间后,我会得到黑屏。
我上网冲浪并发现类似的问题,但这也没有帮助,这里是链接:
http://groups.google.com/group/android-for-beginners/browse_thread/thread/42431dd1ca4a9d98
任何帮助和想法对我来说都会非常有帮助,这对我来说花了太长时间,
因为我正在尝试在 Web 视图中显示我的 Web 应用程序,我只有一个 Activity,其中包含如下代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// so that when launcher is clicked while the application is
// running , the application doesn't start from the begnining
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// Show the ProgressDialog on this thread
this.progressDialog = ProgressDialog.show(this, "Pleas Wait..", "Loading", true);
browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressDialog.isShowing()) {
progressDialog .dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some site")) {
Intent i = new
Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return true;
}
}
});
browser.loadUrl("mysite");
}
}
I have included a web application within android web view , and there is a link in the webpage which opens some other site , when the link is clicked it works fine for the first click, however when clicked for the second time the website is not found ,
the code is :
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some site ")) {
Intent i = new
Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return false;
}
}
@THelper and @mikegr, thanks for the reply,
Actually in my case i have a modal panel (JSF) in my web application which contains some buttons, on clicking the button i am opening some other site using javascript window.open() method which works fine in desktop browser, however, when i wrap this web application within android webview, everything works fine except when i first click this button i'm able to open the other site using the external browser, however on second click the webview tries to open this othersite within the webview instead of the external browser and i get website not found with the entire URL of the other site, this happens even when i logout and login again as the application launched is still running.
also in my case after sometime when the application is idle i get the black screen.
i surfed through the net and found simillar issue but that didn't help either , here is the link:
http://groups.google.com/group/android-for-beginners/browse_thread/thread/42431dd1ca4a9d98
any help and ideas would be very helpful for me, this is taking too long for me,
since i'm trying to display my web application in the web view, i have only one activity, which contains code like this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// so that when launcher is clicked while the application is
// running , the application doesn't start from the begnining
} else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
// Show the ProgressDialog on this thread
this.progressDialog = ProgressDialog.show(this, "Pleas Wait..", "Loading", true);
browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressDialog.isShowing()) {
progressDialog .dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("some site")) {
Intent i = new
Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(i);
return true;
} else {
view.loadUrl(url);
return true;
}
}
});
browser.loadUrl("mysite");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我有过在某些情况下不调用 shouldOverrideUrlLoading() 的经验。
关于这个主题有一些错误
http://code.google.com/p/android/issues
例如错误号 15827、9122、812、2887
作为解决方法,尝试添加方法 onPageStarted() 并检查是否收到此调用。对我来说,即使之前没有调用过 shouldOverrideUrlLoading() ,这个方法也会被调用。
I had the experience that shouldOverrideUrlLoading() is not called in certain circumstances.
There are a few bugs about this topic on
http://code.google.com/p/android/issues
like bug number 15827, 9122, 812, 2887
As a workaround try to add the method onPageStarted() and check if you get this call. For me this method is always called even if shouldOverrideUrlLoading() was not called before.
onPageStarted
为我工作。必须稍微调整一下,因为该方法也是在首次渲染 Web 视图时调用的,并且我只想在横幅的 javascript 的onClick
上执行它。我正在使用自定义页面模拟横幅,因此当呈现
ad.html
时,我避免了startActivity
。否则,它将启动新的浏览器窗口。onPageStarted
worked for me. Had to tweak it a bit, as that method is called when the webview is first rendered too, and I wanted to only execute it on theonClick
of the banner's javascript.I was simulating a banner with a custom page, so when the
ad.html
was being rendered, I avoided thestartActivity
. Otherwise, it launches the new browser window.尝试在加载之前在 url 中附加系统参数模式,例如:
并在 shouldOverrideUrlLoading() 方法中删除查询部分(在第一行)。
Try to append the System parameter pattern in url before you load that, something like.
and in your shouldOverrideUrlLoading() method remove the query part (in a very first line).