遇到web view (WebView)隔段时间就自动刷新问题竟然在网上找不到解决方案,终于在琢磨了几天之后,找到了问题的答案。
其实问题很简单。就是当前activity的onResume()方法被反复执行, 原因未查明(手机root过,可能有这方面原因),解决方法当然就是不要将webview不放在onResume中了。
在onCreate()中就执行页面加载就行了,放在onResume中可能因为某些原因导致反复执行,这里就不分析了。
直接来段代码。
import android.graphics.Bitmap; import android.net.http.SslError; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.webkit.SslErrorHandler; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class TestWebviewActivity extends ActionBarActivity { private WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_webview); webView = (WebView) findViewById(R.id.web_view); webView.getSettings().setJavaScriptEnabled(true); String url = "http://windigniter.com/user/center/uid/1"; webView.requestFocus(); webView.loadUrl(url); webView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { Toast.makeText(getApplicationContext(), newProgress+"", Toast.LENGTH_SHORT).show(); super.onProgressChanged(view, newProgress); } }); webView.setWebViewClient(new WebViewClient() { /*@Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return super.shouldOverrideUrlLoading(view, url); }*/ @Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { handler.proceed(); super.onReceivedSslError(view, handler, error); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(getApplicationContext(), errorCode + " " + description, Toast.LENGTH_SHORT).show(); super.onReceivedError(view, errorCode, description, failingUrl); } }); } @Override protected void onResume() { super.onResume(); } }
(6070)