Android开发中如何衡量当前的网速?
平均URL
包装com.panodic.settings.net;
导入Java . io . buffered reader;
导入Java . io . file;
import Java . io . file not found exception;
导入Java . io . file output stream;
导入Java . io . io exception;
导入Java . io . inputstream;
导入Java . io . inputstreamreader;
导入Java . net . httpurlconnection;
导入Java . net . malformedurexception;
导入Java . net . URL;
导入Java . net . URL connection;
导入com . pano DIC . settings . util . logutil;
导入com . pano DIC . settings . util . net util;
导入com . pano DIC . settings . util . util;
导入com . pano DIC . settings . view . patch item;
导入Android . OS . bundle;
导入Android . OS . handler;
导入Android . OS . message;
导入Android . app . activity;
导入Android . view . menu;
导入Android . view . view;
导入Android . view . view . onclick listener;
导入Android . view . window;
导入Android . widget . button;
导入Android . widget . progress bar;
导入Android . widget . textview;
导入Android . widget . toast;
导入com . pano DIC . settings . r;
公共类MesureSpeed扩展Activity实现OnClickListener {
私有静态最终int LOADING = 0x 111;
私有静态最终int STOP = 0x 112;
私人进展巴毫巴;
private int mProgressState
私有TextView mSpeed
私人按钮测量速度;
private PatchItem mBack
私有浮点mSpeedContent
私有字符串mAddr =“。lgx。phone expert _ 221804。apk“;
private String maddr 2 =“/data/wise game/6f 9153 D4 a8d 1f7d 8/QQ . apk“;
private String maddr 3 =“/data/wise game/Baidu search _ Android _ 10189 _ 1399k . apk“;
private Handler mHandler = new Handler(util . staskrunner . get looper());
private int testCount = 0;
@覆盖
受保护的void onCreate(Bundle saved instance estate ){
super . oncreate(savedInstanceState);
requestWindowFeature(Window。特征号标题);
setContentView(r . layout . activity _ me sure _ speed);
m speed =(TextView)findViewById(r . id . speed _ content);
mMeasureSpeed =(Button)findViewById(r . id . me sure _ speed);
m back =(patch item)findViewById(r . id . patch _ settings _ net _ speed);
mBar =(progress bar)findViewById(r . id . bar);
mmeasuespeed . setonclicklistener(this);
m back . setonclicklistener(this);
test count = 0;
}
@覆盖
公共void onClick(视图v ){
if(m back . is my child(v)){
util . finish(this);
} else if(v = = mMeasureSpeed ){
mmeasuespeed . set enabled(false);
mBar.setVisibility(视图。可见);
mProgressState = 0;
test count = 0;
mbar . set progress(mProgressState);
mhandler . remove callbacks(null);
mhandler . post delayed(new Runnable(){
@覆盖
公共void运行(){
测量速度(mAddr);
}
}, 0);
}
}
私有处理程序mProgressHandler = new Handler(){
@覆盖
公共void handleMessage(消息消息){
开关(消息。内容){
装箱:
mbar . set progress(mProgressState);
打破;
案例停止:
mBar.setVisibility(视图。不见了);
setSpeed();
mmeasuespeed . set enabled(true);
打破;
默认值:
打破;
}
}
};
私有void setSpeed(){
if(mSpeedContent & gt;= 1024) {
mSpeedContent =(float)((mSpeedContent)/(1024+0.0));
mSpeedContent =(float)((int)(mSpeedContent * 10)% 10+0.0)/10+(int)mSpeedContent);
m speed . settext(mSpeedContent+getString(r . string . m));
}否则{
m speed . settext((int)mSpeedContent+getString(r . string . kb));
}
}
私有void measureSpeed(字符串),Toast。LENGTH_SHORT)
。show();
mprogresshandler . sendemptymessage(STOP);
返回;
}
int fileLen = 0;
长start time = 0;
long end time = 0;
最终字符串fileName =“tmp . apk“;
HttpURLConnection conn = null
InputStream为= null
FileOutputStream fos = null
File tmpFile =新文件(“/SD card/temp“);
如果(!tmpfile . exists()){
tmpfile . mkdir();
}
最终文件file =新文件(“/SD card/temp/“+fileName);
尝试{
URL url =新URL(http URL);
尝试{
conn =(HttpURLConnection)URL . open connection();
洛古蒂尔。d(“乐宁“);
fileLen = conn . get contentlength();
logutil . d(“len =“+fileLen);
if(fileLen & lt;= 0) {
mSpeedContent = 0;
mprogresshandler . sendemptymessage(STOP);
toast . make text(this,getString(r . string . conn _ fail),
吐司。长度_短)。show();
返回;
}
start time = system . current time millis();
is = conn . getinputstream();
fos = new file output stream(file);
byte【】buf =新字节【256】;
conn . connect();
if(conn . getresponsecode()& gt;= 400) {
toast . make text(this,getString(r . string . no _ time),
吐司。长度_短)。show();
mprogresshandler . sendemptymessage(STOP);
返回;
}否则{
while(true ){
如果(是!= null) {
int numRead = is . read(buf);
if(numRead & lt;= 0) {
打破;
}否则{
fos . write(buf,0,numRead);
}
mProgressState+=(int)(((numRead+0.0)/(fileLen+0.0))* 1000000);
mprogresshandler . sendemptymessage(正在加载);
//logutil . d(“numRead =“+numRead+“fileLen =“
//+fileLen);
}否则{
打破;
}
}
}
end time = system . current time millis();
} catch(io exception e ){
e . printstacktrace();
toast . make text(this,getString(r . string . no _ permission),
吐司。长度_短)。show();
}最后{
如果(conn!= null) {
conn . disconnect();
}
尝试{
if(fos!= null) {
fos . close();
}
如果(是!= null) {
is . close();
}
} catch(io exception e 1)
e 1 . printstacktrace();
}
}
} catch(malformedurexception e ){
e . printstacktrace();
}
mSpeedContent = fileLen/(end time-start time);
mprogresshandler . sendemptymessage(STOP);
}
}