[每当我尝试从android网络视图中获取加载时间时,对于我加载的任何网站,它都会一直为我提供相同的1.5701998E12
秒,我知道我的互联网很慢,但我认为它并不那么慢。一直在尝试解决问题,但我没有运气。
package com.example.new_app;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
WebView webView;
float end;
final Context context = this;
String UR_L;
String numberAsString;
float start;
float total;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url) {
start = (new Date()).getTime();
}
//This function will take in the finished load time and create an alert
public void onPageFinished(WebView view, String url) {
end = (new Date()).getTime();
total=end-start;
numberAsString = String.valueOf(total);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Load Time");
// set dialog message
alertDialogBuilder
.setMessage(numberAsString)
.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("http://www.Google.com");
}
}
我认为您可以使用System.currentTimeMillis()
来获取时间。它返回一个long
。因此,您需要将start
,end
和total
更改为long