【问题标题】:Webview keeps reloading same pageWebview不断重新加载同一页面
【发布时间】:2017-02-02 09:17:26
【问题描述】:

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

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 新手,因此是我的第一个应用程序

【问题讨论】:

    标签: java android url webview download


    【解决方案1】:

    试试这个代码:在你的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参考

    【讨论】:

    • 感谢您的贡献
    • 乐于助人...thnkx
    【解决方案2】:

    如果想在其他浏览器中打开链接。试试这个。

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

    希望这会有所帮助。:)

    【讨论】:

    • 谢谢,但我必须接受第一个答案,它有更多信息
    【解决方案3】:

    尝试上面的代码在您自己的应用程序中打开 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");
    

    【讨论】:

    • 这段代码和我的有点相似,感谢以上回答解决了它的兴趣
    • 嗯,我已经给出了在我们的应用程序本身中打开 webview 的代码,而不是将用户导航到任何其他 web 浏览器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2020-08-12
    • 2014-04-16
    • 2020-01-27
    • 1970-01-01
    相关资源
    最近更新 更多