Android:单击网页视图中页面中的链接

发布于 2024-12-06 03:20:50 字数 2532 浏览 0 评论 0原文

我在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

handling links in a webview ,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梓梦 2024-12-13 03:20:50

我有过在某些情况下不调用 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.

獨角戲 2024-12-13 03:20:50

onPageStarted 为我工作。必须稍微调整一下,因为该方法也是在首次渲染 Web 视图时调用的,并且我只想在横幅的 javascript 的 onClick 上执行它。

我正在使用自定义页面模拟横幅,因此当呈现 ad.html 时,我避免了 startActivity。否则,它将启动新的浏览器窗口。

WebViewClient newWebClient = new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            if(!url.equals("http://xxxx.ad.html"))
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    };

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 the onClick of the banner's javascript.

I was simulating a banner with a custom page, so when the ad.html was being rendered, I avoided the startActivity. Otherwise, it launches the new browser window.

WebViewClient newWebClient = new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            if(!url.equals("http://xxxx.ad.html"))
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    };
猫腻 2024-12-13 03:20:50

尝试在加载之前在 url 中附加系统参数模式,例如:

String url = "http://xxxx.ad.html?t="+System.nanoTime();

并在 shouldOverrideUrlLoading() 方法中删除查询部分(在第一行)。

int idx;
if ((idx = url.indexOf("?")) != -1) {
    url = url.substring(0, idx);
}

Try to append the System parameter pattern in url before you load that, something like.

String url = "http://xxxx.ad.html?t="+System.nanoTime();

and in your shouldOverrideUrlLoading() method remove the query part (in a very first line).

int idx;
if ((idx = url.indexOf("?")) != -1) {
    url = url.substring(0, idx);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文