2015-10-04 2 views
0

У меня есть приложение для Android, где есть список серверов и код для их ping. Но, когда я пингую один из них, он всегда работает, но для того, чтобы пинговать больше из них подряд или более, чем один (с несколькими секундными сделками), приложение очень неустойчиво, отстает или что-то еще (иногда logcat говорит, что некоторые (600 +) кадры пропущены, слишком много работы на главной теме (хореограф: пропущено 4230 кадров! Приложение может делать слишком много работы над своей основной нитью.)). Приложение отлично работало, когда у меня было всего 1 сервер для пинга, я попытался использовать метод, размещенный в коде здесь, с помощью ping их всех одной кнопкой или ping только 1 половина из них и т. Д. Что не так? (Должно ли быть слишком много onClickListeners? Я думаю, что не потому, что, когда у меня была только одна кнопка для ping, все они делали то же самое). Приложение после щелчка на том, что textView не сбой, но отставание в течение 5 + сек.Android-Нестабильный, медленный, ненадежный PING

Second_Fragment.java

package myapp.testapp; 

import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import java.io.IOException; 
import java.lang.InterruptedException; 
import java.lang.Override; 
import java.lang.Process; 
import java.lang.Runtime; 


public class Second_Fragment extends Fragment { 


    long uae; 
    long brazil; 
    long russia; 
    long eueast; 
    long euwest; 
    long asia; 
    long australia; 
    long useast; 
    long uswest; 
    long africa; 
    long india; 


    View myView2; 


    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView2 = inflater.inflate(R.layout.second_layout, container, false); 


     //click button 
     TextView clickFirstButton = (TextView) myView2.findViewById(R.id.uae); 
     clickFirstButton.setOnClickListener(new View.OnClickListener() { 



      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       pingUAE(); 

       if(pingUAE()) 
       { 

        TextView textView = (TextView)myView2.findViewById(R.id.uaeping); 
        textView.setText("server on, ping is: " + uae + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.uaeping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickRussia = (TextView) myView2.findViewById(R.id.russia); 
     clickRussia.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingRUSSIA(); 

       if(pingRUSSIA()) 
       { 

        TextView textView = (TextView)myView2.findViewById(R.id.russiaping); 
        textView.setText("server on, ping is: " + russia + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.russiaping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickBrazil = (TextView) myView2.findViewById(R.id.brazil); 
     clickBrazil.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingBRAZIL(); 

       if(pingBRAZIL()) 
       { 

        TextView textView = (TextView)myView2.findViewById(R.id.brazilping); 
        textView.setText("server on, ping is: " + brazil + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.brazilping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickEUEast = (TextView) myView2.findViewById(R.id.eueast); 
     clickEUEast.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingEUEAST(); 

       if(pingEUEAST()) 
       { 

        TextView textView = (TextView)myView2.findViewById(R.id.eueastping); 
        textView.setText("server on, ping is: " + eueast + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.eueastping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickEUWest = (TextView) myView2.findViewById(R.id.euwest); 
     clickEUWest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingEUWEST(); 

       if(pingEUWEST()) 
       { 

        TextView textView = (TextView)myView2.findViewById(R.id.euwestping); 
        textView.setText("server on, ping is: " + euwest + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.euwestping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickAsia = (TextView) myView2.findViewById(R.id.asia); 
     clickAsia.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingASIA(); 

       if(pingASIA()) 
       { 

        TextView textView = (TextView)myView2.findViewById(R.id.asiaping); 
        textView.setText("server on, ping is: " + asia + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.asiaping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickAustralia = (TextView) myView2.findViewById(R.id.australia); 
     clickAustralia.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingAUSTRALIA(); 

       if(pingAUSTRALIA()) 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.australiaping); 
        textView.setText("server on, ping is: " + australia + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.australiaping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickUSEast = (TextView) myView2.findViewById(R.id.useast); 
     clickUSEast.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingUSEAST(); 

       if(pingUSEAST()) 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.useastping); 
        textView.setText("server on, ping is: " + useast + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.useastping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickUSWest = (TextView) myView2.findViewById(R.id.uswest); 
     clickUSWest.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingUSWEST(); 

       if(pingUSWEST()) 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.uswestping); 
        textView.setText("server on, ping is: " + uswest + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.uswestping); 
        textView.setText("server off "); 
       } 

      } 
     }); 

     TextView clickAfrica = (TextView) myView2.findViewById(R.id.africa); 
     clickAfrica.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingAFRICA(); 

       if(pingAFRICA()) 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.africaping); 
        textView.setText("server on, ping is: " + africa + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.africaping); 
        textView.setText("server off "); 
       } 

      } 
     }); 


     TextView clickIndia = (TextView) myView2.findViewById(R.id.india); 
     clickIndia.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pingINDIA(); 

       if(pingINDIA()) 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.indiaping); 
        textView.setText("server on, ping is: " + india + "ms"); 
       } 
       else 
       { 
        TextView textView = (TextView)myView2.findViewById(R.id.indiaping); 
        textView.setText("server off "); 
       } 
      } 

     }); 

     return myView2; 
    } 




    public boolean pingUAE() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long a = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); 
      int  exitValue = ipProcess.waitFor(); 
      uae = System.currentTimeMillis() % 1000 - a; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingBRAZIL() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long b = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 209.197.25.1"); 
      int  exitValue = ipProcess.waitFor(); 
      brazil = System.currentTimeMillis() % 1000 - b; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingRUSSIA() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long c = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 146.66.156.1"); 
      int  exitValue = ipProcess.waitFor(); 
      russia = System.currentTimeMillis() % 1000 - c; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingEUEAST() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long d = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 146.66.155.1"); 
      int  exitValue = ipProcess.waitFor(); 
      eueast = System.currentTimeMillis() % 1000 - d; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingEUWEST() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long e = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 146.66.152.1"); 
      int  exitValue = ipProcess.waitFor(); 
      euwest = System.currentTimeMillis() % 1000 - e; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingASIA() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long f = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 103.28.54.1"); 
      int  exitValue = ipProcess.waitFor(); 
      asia = System.currentTimeMillis() % 1000 - f; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingAUSTRALIA() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long g = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 103.10.125.1"); 
      int  exitValue = ipProcess.waitFor(); 
      australia = System.currentTimeMillis() % 1000 - g; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingUSEAST() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long h = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 208.78.164.1"); 
      int  exitValue = ipProcess.waitFor(); 
      useast = System.currentTimeMillis() % 1000 - h; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingUSWEST() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long i = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 192.69.96.1"); 
      int  exitValue = ipProcess.waitFor(); 
      uswest = System.currentTimeMillis() % 1000 - i; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingAFRICA() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long j = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 197.80.200.1"); 
      int  exitValue = ipProcess.waitFor(); 
      africa = System.currentTimeMillis() % 1000 - j; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

    public boolean pingINDIA() { 
     Runtime runtime = Runtime.getRuntime(); 
     try { 
      long k = System.currentTimeMillis() % 1000; 
      Process ipProcess = runtime.exec("/system/bin/ping -c 1 116.202.224.146"); 
      int  exitValue = ipProcess.waitFor(); 
      india = System.currentTimeMillis() % 1000 - k; 
      return (exitValue == 0); 
     } catch (IOException e)   { e.printStackTrace(); } 
     catch (InterruptedException e) { e.printStackTrace(); } 
     return false; 
    } 

} 

second_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 


    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginTop="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/uae" 
      android:id="@+id/uae" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/uaeping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/brail" 
      android:id="@+id/brazil" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/brazilping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/russia" 
      android:id="@+id/russia" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/russiaping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/eu_east" 
      android:id="@+id/eueast" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/eueastping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/eu_west" 
      android:id="@+id/euwest" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/euwestping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/asia" 
      android:id="@+id/asia" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/asiaping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/australia" 
      android:id="@+id/australia" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/australiaping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/us_east" 
      android:id="@+id/useast" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/useastping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/us_west" 
      android:id="@+id/uswest" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/uswestping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/africa" 
      android:id="@+id/africa" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/africaping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

    <TableRow 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="3dp" 
     android:layout_marginBottom="4dp"> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/india" 
      android:id="@+id/india" 
      android:layout_above="@+id/button_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/space" 
      android:id="@+id/indiaping" 
      android:layout_marginStart="3dp" 
      android:layout_marginBottom="4dp" /> 
    </TableRow> 

</TableLayout> 

LogCat

http://pastebin.com/ycK4q2dn

Благодарим за решение.

ответ

1

Каждый раз, когда вы нажимаете кнопку, основной поток пользовательского интерфейса запускает процесс, а затем ждет его завершения, в результате чего поток пользовательского интерфейса приостанавливается. Эта пауза - это то, что заставляет Android сообщать о предупреждениях «слишком много работы с основным потоком» и вашем приложении «отставать».

Вам необходимо изменить методы ping(), чтобы они запускались в отдельных потоках и не вызывали зависание потока пользовательского интерфейса.

Что-то вроде этого:

public class Second_Fragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView2 = inflater.inflate(R.layout.second_layout, container, false); 
     //click button 
     TextView clickFirstButton = (TextView) myView2.findViewById(R.id.uae); 
     clickFirstButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // launch the ping process in a separate thread 
       new Thread(pingUAE).start(); 
      } 
     }); 
     return myView2; 
    } 

    void setPingResult(int id, long pingtime) { 
      TextView textView = (TextView)myView2.findViewById(id); 
      if (pingtime >= 0) { 
       textView.setText("server on, ping is: " + pingtime + "ms"); 
      } 
      else { 
       textView.setText("server off "); 
      } 
    } 


    public Runnable pingUAE = new Runnable() { 
     @Override 
     public void run() { 
      // initialise time to -1 
      uae = -1; 
      Runtime runtime = Runtime.getRuntime(); 
      try { 
       long a = System.currentTimeMillis() % 1000; 
       Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); 
       int  exitValue = ipProcess.waitFor(); 
       uae = System.currentTimeMillis() % 1000 - a; 
      } catch (IOException e)   { e.printStackTrace(); } 
      catch (InterruptedException e) { e.printStackTrace(); } 

      // update the ping result - we need to call this on the UI thread 
      // because it updates UI elements (TextView) 
      getActivity().runOnUIThread(new Runnable() { 
       @Override 
       public void run() { 
        setPingResult(R.id.uae, uae); 
       } 
      }); 

     } 
    } 

} 
+0

Можете ли вы дать мне подсказку? Я уже googled и пробовал некоторые методы, но я не знаю, ho, чтобы сделать это с boolean ... – tonakriz

+0

@ AntonínKříž добавил пример кода – adelphus

+0

Спасибо человеку! Работает! – tonakriz

Смежные вопросы