android webview测量所有资源的次数
android webview measure times of all resources
是否可以测量一个webview所有资源的加载时间?
例如,为了加载完整的网页,我使用:
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startingTime = System.currentTimeMillis();
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
timeElapsed = System.currentTimeMillis() - startingTime;
super.onPageFinished(view, url);
}
但是为了测量CSS、图像等的负载,我使用
public HashMap<String, Long> resources = new HashMap<String, Long>();
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request){
resources.put(request.getUrl().toString(), System.currentTimeMillis());
WebResourceResponse response = super.shouldInterceptRequest(view, request);
return response;
}
public void onLoadResource(WebView view, String url) {
if(resources.containsKey(url)){
Long timeStartResource = resources.get(url);
Long timeElapseResource = System.currentTimeMillis() - timeStartResource;
}
super.onLoadResource(view, url);
}
我认为 onLoadResource 方法是在加载资源时执行的,但根据文档
public void onLoadResource (WebView view, String url).
Notify the host application that the WebView will load the resource specified by the given url.
和
public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)
Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used.
他们都 运行 在加载资源之前。有什么方法可以测量这些时间吗?
您可以使用 onPageFinished() 完成并且已经给出了此问题的答案 description here.
有一个很棒的 HTML5 API 可供您的页面访问。参见 http://www.sitepoint.com/introduction-resource-timing-api/
从 KitKat 开始,WebView 基于 Chromium,支持此 API。要检查这一点,请为您的应用打开 remote web debugging(假设您已为 WebView 启用 JavaScript),然后尝试在控制台中进行评估:
window.performance.getEntriesByType('resource')
对于页面加载的每个资源,您将获得一个 PerformanceResourceTiming
对象数组。
为了将您的信息传输到 Java 端,您可以使用注入的 Java 对象。
是否可以测量一个webview所有资源的加载时间? 例如,为了加载完整的网页,我使用:
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startingTime = System.currentTimeMillis();
super.onPageStarted(view, url, favicon);
}
public void onPageFinished(WebView view, String url) {
timeElapsed = System.currentTimeMillis() - startingTime;
super.onPageFinished(view, url);
}
但是为了测量CSS、图像等的负载,我使用
public HashMap<String, Long> resources = new HashMap<String, Long>();
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request){
resources.put(request.getUrl().toString(), System.currentTimeMillis());
WebResourceResponse response = super.shouldInterceptRequest(view, request);
return response;
}
public void onLoadResource(WebView view, String url) {
if(resources.containsKey(url)){
Long timeStartResource = resources.get(url);
Long timeElapseResource = System.currentTimeMillis() - timeStartResource;
}
super.onLoadResource(view, url);
}
我认为 onLoadResource 方法是在加载资源时执行的,但根据文档
public void onLoadResource (WebView view, String url).
Notify the host application that the WebView will load the resource specified by the given url.
和
public WebResourceResponse shouldInterceptRequest (WebView view, WebResourceRequest request)
Notify the host application of a resource request and allow the application to return the data. If the return value is null, the WebView will continue to load the resource as usual. Otherwise, the return response and data will be used.
他们都 运行 在加载资源之前。有什么方法可以测量这些时间吗?
您可以使用 onPageFinished() 完成并且已经给出了此问题的答案 description here.
有一个很棒的 HTML5 API 可供您的页面访问。参见 http://www.sitepoint.com/introduction-resource-timing-api/
从 KitKat 开始,WebView 基于 Chromium,支持此 API。要检查这一点,请为您的应用打开 remote web debugging(假设您已为 WebView 启用 JavaScript),然后尝试在控制台中进行评估:
window.performance.getEntriesByType('resource')
对于页面加载的每个资源,您将获得一个 PerformanceResourceTiming
对象数组。
为了将您的信息传输到 Java 端,您可以使用注入的 Java 对象。