-3

I am doing an exercise in Android and I want to display value of a ping. I followed an existing answer (this topic). The fact is that the code that the person gave doesn't display any information (I toasted it to test the return value of the function "ping"). I called the function like this :

Toast.makeText(getApplicationContext(), ping("http://www.stackoverflow.com"), Toast.LENGTH_SHORT).show();

Any other kind of code to simply display ping value ?

I precise that my code is free of any other process, I just want to finally display this result in a small textView.

Here is my code.

MainActivity.java

private String ping(String url) {

    String str = "";

    try {
        Process process = Runtime.getRuntime().exec(
                "/system/bin/ping -c 8 " + url);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        int i;
        char[] buffer = new char[4096];
        StringBuffer output = new StringBuffer();
        while ((i = reader.read(buffer)) > 0)
            output.append(buffer, 0, i);
        reader.close();

        // body.append(output.toString()+"\n");
        str = "ping : " + output.toString();
        // Log.d(TAG, str);
    } catch (IOException e) {
        // body.append("Error\n");
        e.printStackTrace();
    }

    return str;
}

private Button button;
private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.textView);
    button = (Button) findViewById(R.id.button);

    toggleButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), ping("http://www.stackoverflow.com"), Toast.LENGTH_SHORT).show();
            });                     
    }); }
Anwar
  • 4,162
  • 4
  • 41
  • 62

3 Answers3

1

Try something like here: Android- Unstable, slow, unreliable PING

After public class NameOfYourActivity

// you need to define this value here, you will need it between "voids", "booleans", ...
long timeofping;

OnCreate:

// button on click listener
Button clickMyButton = (Button)findViewById(R.id.button);
clickFirstButton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            // launch the ping process in a separate thread
            // beacuse of the stability of the app
            new Thread(ping).start();
        }
});

After method "onCreate"

// this code runs after the code bellow ("public Runnable ping ...")
void setPingResult(long pingtime) {
    Toast.makeText(getApplicationContext(), timeofping + " milliseconds", Toast.LENGTH_LONG).show();
}



// thread of "ping" process
public Runnable ping = new Runnable() {
        @Override
        public void run() {
            Runtime runtime = Runtime.getRuntime();
            try {
                long a = System.currentTimeMillis() % 1000;
                Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
                ipProcess.waitFor();
                timeofping = 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)
            runOnUiThread(new Runnable() {
            public void run() {
                setPingResult(timeofping);
            }
        });

    }
}

Please fix me, if there are any errors! Dont forget "imports"! (etc import android.widget.Toast;)

Community
  • 1
  • 1
antoninkriz
  • 966
  • 4
  • 18
  • 36
  • Oh gosh ! Thank you for answering Antonin, I have specialized in HTML/CSS since a long time now but I think i'll go back to Android for a while and I just needed this ! :) – Anwar Oct 09 '15 at 19:24
  • @Zeratops If this works for you, please check this answer as "answered". Thanks – antoninkriz Oct 11 '15 at 19:05
-1
yourTextView.setText(ping("http://www.stackoverflow.com"));
Sparrow_ua
  • 664
  • 1
  • 11
  • 28
  • Thanks for answering Sparrow_ua, but if the ping result doesn't display in a Toast I have big doubt on the result of your code. – Anwar Jul 10 '14 at 21:38
-1

Found my error : need to use IP adresse like 8.8.4.4instead of domain adress like www.google.com. Maybe there is a DNS permission to need ?

Anwar
  • 4,162
  • 4
  • 41
  • 62