Android- PING 不稳定、缓慢、不可靠

Android- Unstable, slow, unreliable PING

我有 android 应用程序,服务器列表和 ping 它们的代码在哪里。但是,当我对其中一个进行 ping 操作时,它始终有效,但是对于连续 ping 多个或多个(处理几秒钟)应用程序非常不稳定、滞后或其他任何情况(有时 logcat 说一些(600+)帧因为主线程上的工作太多而跳过(编排:跳过 4230 帧!应用程序可能在其主线程上做了太多工作。))。当我只有 1 台服务器要 ping 时,应用程序运行良好,我尝试使用此处代码中发布的方法,通过一个按钮全部 ping 它们或仅 ping 其中的一半等。有什么问题吗? (它应该太多 onClickListeners 吗?我认为不是,因为当我只有一个按钮来 ping 所有它们时,它做了同样的事情)。单击该 textView 后的应用程序不会崩溃,但会滞后 5 秒以上。

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

感谢您提供任何解决方案。

每次按下按钮时,主 UI 线程都会启动一个进程,然后等待它完成,从而导致 UI 线程暂停。此暂停导致 Android 向 "lag" 报告 "too much work on main thread" 警告和您的应用程序。

您需要更改您的 ping() 方法,以便它们在单独的线程中启动并且不会导致您的 UI 线程等待。

像这样:

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);
                }
            });

        }
    }

}