我有一个应用程序从文本文件中读取网站,然后计算将网页完全加载到 webview 中所需的时间。我尝试通过在将 url 加载到 webview 之前获取时间(以毫秒为单位)来计算时间,然后在网页完成加载 webview 后获取时间并获取两者之间的差异,但它始终为 0。任何想法为什么?加载一个网页的时间应该大于0?
public class MainActivity extends Activity {
// NUMSITES will be the number of websites contained in the sites.txt file.
int NUMSITES = 4;
int curSite = 0;
long startTime = 0;
long endTime = 0;
long totTime = 0;
boolean start = true;
Calendar cal = Calendar.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
final WebView wb = (WebView) findViewById(R.id.webView1);
final String[] sites = new String[NUMSITES];
getWebsites(sites);
// Listed as optimal settings for HTML5 (may need testing?).
// Ref.
// http://stackoverflow.com/questions/10097233/optimal-webview-settings-for-html5-support
wb.getSettings().setJavaScriptEnabled(true);
WebSettings settings = wb.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
wb.setFocusable(true);
wb.setFocusableInTouchMode(true);
wb.getSettings().setRenderPriority(RenderPriority.HIGH);
wb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wb.getSettings().setDatabaseEnabled(true);
wb.getSettings().setAppCacheEnabled(true);
wb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebViewClient client = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return true;
}
@Override
public void onLoadResource(WebView view, String url) {
}
@Override
public WebResourceResponse shouldInterceptRequest(WebView view,
String url) {
return null;
}
// On page finished, retrive next website from string array and load
// it.
@Override
public void onPageFinished(WebView view, String url) {
//
endTime = cal.getTimeInMillis();
totTime = endTime - startTime;
Log.d("Time", "Time: " + totTime);
Log.d("Time", "Start Time: " + startTime);
Log.d("Time", "End Time: " + endTime);
if (curSite < NUMSITES) {
startTime = cal.getTimeInMillis();
wb.loadUrl(sites[curSite]);
curSite++;
}
}
};
wb.setWebViewClient(client);
// Initial webpage
wb.loadUrl("https://sites.google.com/site/imagesizetesting/one-1");
}