Android-不稳定,缓慢,不可靠的PING

时间:2015-10-04 16:56:27

标签: java android ping lag

我有Android应用程序,其中列出了服务器和代码来ping它们。但是,当我ping其中一个时,它总是有效,但是对于连续多次ping它们或者多一个(几秒钟),应用程序非常不稳定,滞后或者其他什么(有时logcat说有些(600) +)帧被跳过因为在主线程上工作太多(Choreographer:跳过4230帧!应用程序可能在其主线程上做了太多工作。))。当我只有1个服务器ping时,应用程序工作得很好,我试图使用代码在这里发布的metod,用一个按钮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

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

1 个答案:

答案 0 :(得分:1)

每次按下按钮,主UI线程都会启动一个进程,然后等待它完成,从而导致UI线程暂停。这个暂停是导致Android在主线程上报告过多工作的原因&#34;警告和你的应用程序到了&#34;滞后&#34;。

您需要更改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);
                }
            });

        }
    }

}