gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:06:04 28 4
gpt4 key购买 nike

我有 Android 应用程序,其中有服务器列表和 ping 它们的代码。但是,当我 ping 其中一个时,它总是有效,但是对于连续 ping 多个或多个(需要几秒钟)该应用程序非常不稳定,滞后或其他什么(有时 logcat 说一些(600 +) 由于主线程上的工作太多而跳过了帧(Choreographer:跳过了 4230 帧!应用程序可能在其主线程上做了太多的工作。))。当我只有 1 台服务器需要 ping 时,应用程序工作得很好,我尝试使用此处代码中发布的方法,通过一个按钮对所有服务器进行 ping 操作,或者仅对其中的 1 个服务器进行 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 报告“主线程工作过多”警告以及您的应用程序“滞后”的原因。

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

}
}

}

关于java - Android——PING 不稳定、缓慢、不可靠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32935966/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com