如何刷新webview的内容

时间:2017-04-02 22:29:31

标签: java android webview

如何刷新网页浏览的内容? 这是我的代码,有更新它的方法吗?有办法自动更新页面吗?非常感谢

public class MainActivity extends Activity implements AdvancedWebView.Listener {

private static final String TEST_PAGE_URL = "";
private AdvancedWebView mWebView;

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

    mWebView = (AdvancedWebView) findViewById(R.id.webview);
    mWebView.setListener(this, this);
    mWebView.setGeolocationEnabled(false);
    mWebView.setMixedContentAllowed(true);
    mWebView.setCookiesEnabled(true);
    mWebView.setThirdPartyCookiesEnabled(true);
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            Toast.makeText(MainActivity.this, "Finished loading", Toast.LENGTH_SHORT).show();
        }

    });
    mWebView.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            Toast.makeText(MainActivity.this, title, Toast.LENGTH_SHORT).show();
        }

    });
    mWebView.addHttpHeader("X-Requested-With", "");
    mWebView.loadUrl("");
}

@SuppressLint("NewApi")
@Override
protected void onResume() {
    super.onResume();
    mWebView.onResume();
    // ...
}

@SuppressLint("NewApi")
@Override
protected void onPause() {
    mWebView.onPause();
    // ...
    super.onPause();
}

@Override
protected void onDestroy() {
    mWebView.onDestroy();
    // ...
    super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    mWebView.onActivityResult(requestCode, resultCode, intent);
    // ...
}

@Override
public void onBackPressed() {
    if (!mWebView.onBackPressed()) { return; }
    // ...
    super.onBackPressed();
}

@Override
public void onPageStarted(String url, Bitmap favicon) {
    mWebView.setVisibility(View.INVISIBLE);
}

@Override
public void onPageFinished(String url) {
    mWebView.setVisibility(View.VISIBLE);
}

@Override
public void onPageError(int errorCode, String description, String failingUrl) {
    Toast.makeText(MainActivity.this, "onPageError(errorCode = "+errorCode+",  description = "+description+",  failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
}

@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
    Toast.makeText(MainActivity.this, "onDownloadRequested(url = "+url+",  suggestedFilename = "+suggestedFilename+",  mimeType = "+mimeType+",  contentLength = "+contentLength+",  contentDisposition = "+contentDisposition+",  userAgent = "+userAgent+")", Toast.LENGTH_LONG).show();

    /*if (AdvancedWebView.handleDownload(this, url, suggestedFilename)) {
        // download successfully handled
    }
    else {
        // download couldn't be handled because user has disabled download manager app on the device
    }*/
}
@Override
public void onExternalPageRequest(String url) {
    Toast.makeText(MainActivity.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:0)

WebView有一个可以调用的重载方法。这将重新加载当前网址。

https://developer.android.com/reference/android/webkit/WebView.html#reload()

您想要刷新的时间并不完全清楚,但您需要将其放在代码中的某处:mWebView.reload();