I have android app, where's list of servers and code to ping them. But, I when I ping one of them, it always works, but for pinging more of them in a row or more then one (with few seconds dealy) the app is very unstable, lags or whatever (sometimes logcat says that some (600+) frames skipped becouse too much work on main thread (Choreographer: Skipped 4230 frames! The application may be doing too much work on its main thread.)). App worked perfectly when i had only 1 server to ping, I tried to use the metod posted in code here, with ping them all by one button or ping only 1 half of them etc. Whats wrong? (Should it be too many onClickListeners? I think n not because when i had only one button to ping all of them it did same). The app after click on that textView does not crash, but lag for 5+ sec.
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
Thank you for any solution.
Each time you press a button, your main UI thread is launching a process and then waiting for it to finish, causing the UI thread to pause. This pause is what is causing Android to report the "too much work on main thread" warnings and your App to "lag".
You need to change your ping()
methods so they are launched in separate threads and do not cause your UI thread to wait.
Something like this:
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);
}
});
}
}
}