最近公司要写android与Html5结合的东西,方便以后管理我们的APP
今天需要测试一下页面加载到手机上的时间,原来我经常使用的是TextView,但是还得用Handler,比较麻烦,所以就换成了Chronometer,这东西现在看来就一个缺点,就是他无法精确到
毫秒值,其他都很好!也很好控制!
好了,说正事儿,想获取WebView从开始加载到渲染结束的时间,我们首先要想到的是获取WebView加载结束完成的事件,然后将这段时间记录下来,然后显示出来,先看下效果图:
就像上图,"www.baidu.com"为例,上面的时间就是加载完百度页面之后的时间
原理和思路介绍完了,代码上!are you ready?
MainActivity代码:
package com.example.timedemo;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText;
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.go);
editText = (EditText) findViewById(R.id.edit_query);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
url = editText.getText().toString();
if(url.equals("")){
Toast.makeText(MainActivity.this,"网址不能为空!",Toast.LENGTH_SHORT).show();
}
//跳到一个WebView的Activity
Intent intent = new Intent();
intent.putExtra("url",url);
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
// openBrawse(url,true);
}
});
}
//打开浏览器
public void openBrawse(String url, boolean isFinish) {
if (TextUtils.isEmpty(url)) return;
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
}
package com.example.timedemo;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Chronometer;
import android.widget.TextView;
/**
* 作者:keith on 2016/7/8 10:39
* 邮箱:[email protected]
*/
public class SecondActivity extends AppCompatActivity {
private static final int START = 100;
private static final int STOP = 200;
private WebView webview;
private TextView tv;
private long start;
private long end;
private boolean flag = false;
/**
* 获取用户填入的url
*/
private String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
tv = (TextView) findViewById(R.id.chronometer);
url = getIntent().getStringExtra("url");
start();
initView();
}
private void initView() {
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(url);
webview.setWebViewClient(new MyWebView());
}
/**
* 自定义WebView的WebViewClient,为了获取完成事件
*/
private class MyWebView extends WebViewClient {
private SharedPreferences sp;
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pause();
}
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
end = System.currentTimeMillis() - start;
tv.setText(toTime(end));
}
};
public class MyThread implements Runnable {
@Override
public void run() {
while (flag) {
try {
Thread.sleep(100);// 线程暂停10秒,单位毫秒
Message message = handler.obtainMessage();
message.what = 1;
handler.sendMessage(message);// 发送消息
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void pause() {
flag = false;
}
public void start() {
start = System.currentTimeMillis();
flag = true;
new Thread(new MyThread()).start();
}
private String toTime(long start) {
long millisecond = start % 1000;
millisecond /= 100;
start /= 1000;
long minute = start / 60;
long second = start % 60;
minute %= 60;
return String.format("%02d:%02d:%01d", minute, second, millisecond);
}
}
够详细了吧?
不过这里面会有一个小问题,就是你快速的返回主页,然后继续进行的时候,时间可能会停止不动,这是因为WebView的缓存在作祟;
所以我们可以在设置WebView的时候加上这一句话:
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);就OK了!