Webview不断重新加载相同的页面

时间:2017-02-02 09:17:26

标签: java android url webview download

如果在网站内点击链接时,如何阻止我的以下代码重新加载同一页面?

protected void fetch_web(){
    WebView web;
    web = (WebView) findViewById(R.id.voice_mail_view_port);
    NoNetwork = (Button) findViewById(R.id.btn_no_internet);
    NoNetwork.setVisibility(View.GONE);
    String mdb = "http://192.168.23.1/default.php?appid=1.0";
    getWindow().setFeatureInt(  Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    final Activity MyActivity = this;
    web.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {

            MyActivity.setTitle("Fetching feeds...");
            MyActivity.setProgress(progress * 100);

            loader();

            if(progress == 100) {
                MyActivity.setTitle(R.string.app_name);
                deLoader();;
            }
        }
    });
    web.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String
                failingUrl) {
            deLoader();
            alert("No Internet Connection","Let's get connected to see feeds");
            //web.setVisibility(View.GONE);
            NoNetwork.setVisibility(View.VISIBLE);
        }
    });
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl((mdb));
}
protected  void loader(){
    loading = (ProgressBar) findViewById(R.id.loader);
    loading.setVisibility(View.VISIBLE);
}
protected  void  deLoader(){
    loading = (ProgressBar) findViewById(R.id.loader);
    loading.setVisibility(View.INVISIBLE);
}
protected  void onStart(){
    fetch_web();
    super.onStart();
}

当我添加自定义错误message code

时启动
    web.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String
                failingUrl) {
            deLoader();
            alert("No Internet Connection","Let's get connected to see feeds");
            //web.setVisibility(View.GONE);
            NoNetwork.setVisibility(View.VISIBLE);
        }
    });

我的期望是允许该应用在手机中安装的其他浏览器中打开该链接,或在同一个应用中打开它。

注意:webview上显示的所有链接均为下载链接

您看到要更改或添加的任何帮助,我只是Java的新手,因此我的第一个应用

3 个答案:

答案 0 :(得分:0)

如果想在其他浏览器中打开链接,请执行此操作。

    Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(your_web_url));
startActivity(intent);

希望这会有所帮助。:)

答案 1 :(得分:0)

尝试此代码:在new WebViewClient()

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.example.com")) {
        // This is your web site, so do not override; let WebView load the page
        return false;
    }
    // Otherwise, the link is not for a page on your site, so launch another Activity that handles URLs
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
    return true;
}

shouldOverrideUrlLoading参考

答案 2 :(得分:0)

尝试以上代码在您自己的应用程序中打开url: xml文件:

<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

java文件:

private WebView webView;
webView = (WebView) findViewById(R.id.web_view);`
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://google.com");