当我用一页网页视图刷新网页视图时,每次将其重定向到首页URL时!为什么?

时间:2018-07-04 04:34:14

标签: android webview swiperefreshlayout

这是我的代码........

这里有两个问题

  1. 当我最小化此应用程序窗口并在手机上启动另一个应用程序时,再次查看此应用程序时,在我最小化此应用程序的地方无法恢复。

  2. 当我在此应用程序Webview的任何页面上刷新时,其重定向 每次首页

我想像youtube一样使用progressBar,但是我无法执行可运行的horizo​​ntalBar。

活动main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/root_layout"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android.eforever.MainActivity">

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <WebView
        android:id="@+id/webViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>
    </android.support.v4.widget.SwipeRefreshLayout>
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="310dp"
        />

</RelativeLayout>

主要活动。java

package com.android.eforever;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private WebView webView;
    private ProgressBar mProgress;
    private SwipeRefreshLayout swipeRefreshLayout;

    private String currentUrl="https://www.example.com";

    @Override
    protected void onSaveInstanceState(Bundle bundle) {
        webView.saveState(bundle);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LoadWeb(currentUrl);
            }
        });

        LoadWeb("https://www.example.com/");

    }
    public void LoadWeb(String url) {

        webView = (WebView) findViewById(R.id.webViewId);
        mProgress = (ProgressBar) findViewById(R.id.progressBar);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webView.getSettings().setAppCacheEnabled(true);
        webView.getSettings().setAllowFileAccess(true);
        webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webView.getSettings().setAppCacheEnabled(true);
        webSettings.setEnableSmoothTransition(true);
        webView.setScrollbarFadingEnabled(true);
        webView.loadUrl("url");
        webView.setWebViewClient(new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                currentUrl=url;
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        webView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                Toast.makeText(MainActivity.this, "Opening your default browser and then will start your download. Thank you", Toast.LENGTH_LONG).show();
            }
        });
        webView.setWebViewClient(new CustomWebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                String defaulturl = "https://www.example.com/";
                // all links  with in ur site will be open inside the webview
                //links that start ur domain example(https://www.aamaricche.com/)
                if (url != null && url.startsWith(defaulturl)) {
                    return false;
                }
                // all links that points outside the site will be open in a normal android browse

                else if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                    startActivity(intent);
                    return true;
                } else if (url.startsWith("mailto:")) {
                    Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
                    startActivity(intent);
                    return true;
                } else {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                webView.loadUrl("file:///android_asset/error.html");
            }

            public void onPageFinished(WebView view, String url) {
                mProgress.setVisibility(View.GONE);
                swipeRefreshLayout.setRefreshing(false);
            }
        });
}

    private class CustomWebViewClient extends WebViewClient {
        boolean onPageStarted = false;
        Runnable hideLoadingRunnable = new Runnable() {
            @Override
            public void run() {
                if (!onPageStarted) {
                    mProgress.setVisibility(View.GONE);
                }
            }
        };

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            onPageStarted = true;
            mProgress.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            onPageStarted = false;
            webView.postDelayed(hideLoadingRunnable, 1000);

        }
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

}

2 个答案:

答案 0 :(得分:0)

在给定代码中的loadWeb()方法中存在2个问题

  
      
  1. 有webView.loadUrl(“ url”);但是应该是webView.loadUrl(url);
  2.   

错误的主要原因是您在loadWeb()中两次覆盖了webViewClient

所以最终,您的loadWeb()方法应如下所示。

  public void LoadWeb(String url) {

    webView = (WebView) findViewById(R.id.webViewId);
    mProgress = (ProgressBar) findViewById(R.id.progressBar);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
    webView.getSettings().setAppCachePath(getApplicationContext().getCacheDir().getAbsolutePath());
    webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webView.getSettings().setAppCacheEnabled(true);
    webSettings.setEnableSmoothTransition(true);
    webView.setScrollbarFadingEnabled(true);
    webView.loadUrl(url);
    webView.setDownloadListener(new DownloadListener() {
        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            Toast.makeText(MainActivity.this, "Opening your default browser and then will start your download. Thank you", Toast.LENGTH_LONG).show();
        }
    });
    webView.setWebViewClient(new CustomWebViewClient() {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            String defaulturl = "https://www.example.com/";
            currentUrl=url;
            // all links  with in ur site will be open inside the webview
            //links that start ur domain example(https://www.aamaricche.com/)
            if (url != null && url.startsWith(defaulturl)) {
                return false;
            }
            // all links that points outside the site will be open in a normal android browse

            else if (url.startsWith("tel:")) {
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
                startActivity(intent);
                return true;
            } else if (url.startsWith("mailto:")) {
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
                startActivity(intent);
                return true;
            } else {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
        }

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/error.html");
        }

        public void onPageFinished(WebView view, String url) {
            mProgress.setVisibility(View.GONE);
            swipeRefreshLayout.setRefreshing(false);
        }
    });
}

答案 1 :(得分:0)

每次用户导航时保存重定向 url 的一种方法

private String currentUrl="http://www.example.com";

webview.setWebViewClient(new WebViewClient()
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        currentUrl=url;
        return super.shouldOverrideUrlLoading(view, url);
    }
});