gpt4 book ai didi

android - 每当我尝试从 android web View 中获取加载时间时,它都会给我错误的数字

转载 作者:行者123 更新时间:2023-11-30 05:00:49 25 4
gpt4 key购买 nike

每当我尝试从 android web View 获取加载时间时,对于我加载的任何网站,它都会给我相同数量的 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。因此,您需要将 startend 更改为 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);
...
}
});
}
}

关于android - 每当我尝试从 android web View 中获取加载时间时,它都会给我错误的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com