每当我试图从android web视图中获取加载时间时,它都会给我错误的号码
每当我尝试从android网络视图中获取加载时间时,它总是给我相同数量的每当我试图从android web视图中获取加载时间时,它都会给我错误的号码,android,url,android-webview,performance-testing,timing,Android,Url,Android Webview,Performance Testing,Timing,每当我尝试从android网络视图中获取加载时间时,它总是给我相同数量的1.5701998E12秒,对于我加载的任何网站,我知道我的互联网很慢,但我认为它没有那么慢。我一直在尝试解决问题,但我没有运气 package com.example.new_app; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.Co
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
更改为long
public class MainActivity extends AppCompatActivity {
long end;
long start;
float total;
...
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url) {
start = System.currentTimeMillis();
}
public void onPageFinished(WebView view, String url) {
end = System.currentTimeMillis();
total = end - start;
// Total is the difference in milliseconds.
// Dividing by 1000, you convert it to seconds
numberAsString = String.valueOf(total / 1000);
...
}
});
}
}
我想您可以使用
System.currentTimeMillis()
来获取时间。它返回一个long
。因此,您需要将start
和end
更改为long
public class MainActivity extends AppCompatActivity {
long end;
long start;
float total;
...
webView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url) {
start = System.currentTimeMillis();
}
public void onPageFinished(WebView view, String url) {
end = System.currentTimeMillis();
total = end - start;
// Total is the difference in milliseconds.
// Dividing by 1000, you convert it to seconds
numberAsString = String.valueOf(total / 1000);
...
}
});
}
}
我试过了,现在我的返回号码是十亿,时间是毫秒。除以1000,时间以秒为单位。我更新了答案,数字仍然是胡戈改为浮动总数。对我来说,这个代码是有效的。我得到:总计:3.586开始:157046584873结束:15704658459因为我的开始是0出于某种原因,我尝试了,我的返回号码现在以十亿为单位,时间以毫秒为单位。除以1000,时间以秒为单位。我更新了答案,数字仍然是胡戈改为浮动总数。对我来说,这个代码是有效的。我得到了:总数:3.586开始:157046584873结束:15704658459因为我的开始是0出于某种原因