Android client selects one of the fastest responding servers

Source: Internet
Author: User

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"};         /**     *      * Test Ping success Time      *      *  @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  success    {     long pingsuccEsstime=system.currenttimemillis ()-nowtime;    log.e ("Ping", addr+pingsuccesstime+ ""); &NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;PINGSUCCESSTIME;&NBSP;&NBSP;&NBSP;}ELSE{&NBSP;&NBSP;&NBSP;&NBSP;LOG.E (" Ping ", addr+" Pingsuccesstime: "+55554);    return 55554;   }      } catch  (ioexception e)  {   e.printstacktrace ();   } catch  (interruptedexception e)  {   e.printstacktrace ();   } finally {   proc.destroy (); &NBSP;&NBSP;}&NBSP;&NBSP;LOG.E ("Ping", addr+ " Pingsuccesstime: "+55554);  return 55554;        }                                              &nbsp, @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;    }      &NBSP;&NBSP;&NBSP;LOG.E ("ping", "shortest" +index);    }          }

Android Client selects one of the fastest responding servers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.