标签:
package com.csst.activity; import java.io.IOException; import com.csst.drawer.R; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.SystemClock; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * Looper,MeeageQueue! * * @author Administrator * */ public class LooperDemoActivity extends Activity { protected static int messageQueueIndexId=0; private String [] serverAdd={"www.cnblogs.com","www.iconpng.com","www.baidu.com","developers.google.com"}; /** * *测试ping成功的时间 * * @param addr * @return */ public long pingSuccessTime(String addr){ Runtime run = Runtime.getRuntime(); Process proc = null; long nowtime=System.currentTimeMillis(); try { String str = "ping -c 1 -i 0.2 -W 1 "+ addr; System.out.println(str); proc = run.exec(str); int result = proc.waitFor(); if(result == 0) //ping 成功 { long pingSuccesstime=System.currentTimeMillis()-nowtime; Log.e("ping",addr+pingSuccesstime+""); return pingSuccesstime; }else{ Log.e("ping",addr+"pingSuccesstime:"+55554); return 55554; } } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { proc.destroy(); } Log.e("ping",addr+"pingSuccesstime:"+55554); return 55554; } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.looper_demo_activity); long timeTemp=55555; int index=0; for(int i=0;i<serverAdd.length;i++){ long time=pingSuccessTime(serverAdd[i]); if(time<timeTemp){ index=i; } timeTemp=time; } Log.e("ping","最短"+index); } }
标签:
原文地址:http://my.oschina.net/zengliubao/blog/489546