Home > Java > javaTutorial > body text

Android applet used to check network and network speed

大家讲道理
Release: 2016-11-11 10:45:46
Original
2013 people have browsed it

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
  
public class MainActivity extends Activity {
    private MyHandler handler;
    private Map<String, String> urlMap;
    private Map<String, Integer> idMap;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // final Map<String, String> urlMap = new HashMap<String, String>();
        urlMap = new HashMap<String, String>();
        urlMap.put("tv_q", "http://xw.qq.com/");
        urlMap.put("tv_taobao", "http://m.taobao.com/");
        urlMap.put("tv_baidu", "http://m.baidu.com/");
        urlMap.put("tv_sina", "http://sina.cn/");
        urlMap.put("tv_google", "http://www.google.com.hk/");
        urlMap.put("tv_apple", "http://www.apple.com/");
  
        idMap = new HashMap<String, Integer>();
        idMap.put("tv_q", R.id.tv_q);
        idMap.put("tv_taobao", R.id.tv_taobao);
        idMap.put("tv_baidu", R.id.tv_baidu);
        idMap.put("tv_sina", R.id.tv_sina);
        idMap.put("tv_google", R.id.tv_google);
        idMap.put("tv_apple", R.id.tv_apple);
        Button button = (Button) findViewById(R.id.button1);
        handler = new MyHandler();
  
        // 设置监听
        button.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Iterator<Entry<String, String>> it = urlMap.entrySet()
                        .iterator();
                while (it.hasNext()) {
                    String speed = "正在检测..";
                    Entry<String, String> e = it.next();
                    String id = e.getKey();
                    String url = e.getValue();
                    TextView tv = (TextView) MainActivity.this
                            .findViewById(idMap.get(id));
                    tv.setText(speed);
                }
  
                new MyThread().start();
            }
        });
    }
  
    class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            TextView tv = (TextView) findViewById(msg.what);
            tv.setText(msg.getData().getString("speed"));
        }
    }
  
    class MyThread extends Thread {
        @Override
        public void run() {
            Iterator<Entry<String, String>> it = urlMap.entrySet().iterator();
            while (it.hasNext()) {
                String speed = "访问失败";
                Entry<String, String> e = it.next();
                String id = e.getKey();
                String url = e.getValue();
                long time = 0;
                String result = "";
                try {
                    long start = System.currentTimeMillis();
                    Document doc = Jsoup.connect(url).get();
                    long end = System.currentTimeMillis();
                    time = end - start;
                    result = doc.body().html();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                if (result.length() > 0) {
                    long len = result.getBytes().length;
                    speed = speed(time, len);
                }
                Message message = new Message();
                Bundle bundle = new Bundle();
                bundle.putString("speed", speed);
                message.setData(bundle);
                message.what = idMap.get(id);
                handler.sendMessage(message);
            }
        }
  
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
  
    private String speed(long time, long len) {
        String speed = "";
        if (time > 0) {
            long s = len * 1000 / time; // B/ms
            speed = s + "B/s";
            if (s > 1024) {
                s = s / 1024;
                speed = s + "KB/s";
            }
            if (s > 1024) {
                s = s / 1024;
                speed = s + "MB/s";
            }
            if (s > 1024) {
                s = s / 1024;
                speed = s + "GB/s";
            }
            String size = len + "B";
            if (len > 1024) {
                len = len / 1024;
                size = len + "KB";
            }
            if (len > 1024) {
                len = len / 1024;
                size = len + "MB";
            }
            String t = time + "ms";
            if (time > 1000) {
                time = time / 1000;
                t = time + "sec";
  
                if (time > 60) {
                    time = time / 60;
                    t = time + "min";
                }
            }
            speed = speed + "(" + size + ", " + t + ")";
        }
        return speed;
    }
  
}
Copy after login

Android applet used to check network and network speed

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template