2

I'm sorry for posting similar question to my previous one, but I just can't figure this out.

I've been using simple "ping" example I found here and just wanted to add ProgressBar but with no luck. I really don't understand what's going on.

So, this works fine:

    protected void onPreExecute() {
    sb = new StringBuilder();
    mPOut = new PipedOutputStream();
    try {
        mPIn = new PipedInputStream(mPOut);
        mReader = new LineNumberReader(new InputStreamReader(mPIn));
    } catch (IOException e) {
        cancel(true);
    }
    //myBar.setVisibility(View.VISIBLE); -> PROBLEM!!!
}

protected Object doInBackground(Object... arg0) {
    try {
        process = Runtime.getRuntime().exec("ping -c 4 " + ipadd);
        InputStream in = process.getInputStream();
        OutputStream out = process.getOutputStream();
        byte[] buffer = new byte[1024];
        int count;
        while ((count = in.read(buffer)) != -1) {
            mPOut.write(buffer, 0, count);
            String bs= new String(buffer);
            publishProgress(); 
        }
        in.close();
        out.close();
        mPOut.close();
        mPIn.close();
    } catch (IOException e) {
    }
    return null;
}

protected void onPostExecute(Object result) {
            myBar.setVisibility(View.INVISIBLE);
            tv.setText(sb);
            System.out.println(sb);
}

I get output from ping, myBar is obviously not diplayed since it is invisible in the first place.

If I remove the comment from the line I marked as a problem (set progress bar visibility to visible), I simply get no output from ping. I seems that it somehow messes up my I/O or something. ProgressBar is displayed and hidden at the end, but no output.

I really can't figure this out, so if you have any idea, I would really appreciate any help.

Thanks!!!

Community
  • 1
  • 1
newman555p
  • 181
  • 1
  • 4
  • 13
  • possible duplicate of [Progressbar togther with asyncTask](http://stackoverflow.com/questions/4119009/progressbar-togther-with-asynctask) – Siddharth Lele Jan 23 '13 at 13:14

2 Answers2

2

Try to create programmatically a ProgressDialog in OnPreExecute() method and dismiss it once task is finished in onPostExecute(). Here is a code:

add to onPreExcute:
    super.onPreExecute();
    ProgressDialog pDialog = new ProgressDialog(YourActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

add to onPostExecute:
   pDialog.dismiss();
Marcin S.
  • 11,161
  • 6
  • 50
  • 63
  • Sorry for late response, but I just tried it and it doesn't work. Same thing as when I use progress bar. Dialog is shown and after is hidden, but no output from ping shell comamnd. – newman555p Sep 02 '12 at 13:05
1

Did you override onProgressUpdate ? If not override onProgressUpdate like below and update your progress bar/text view whatever you want in this method.

@Override
protected void onProgressUpdate(String... values) {
    //update your progressbar here
someView.setText(values[0]);    
}

from your code its visible you have called publishProgress but you are not passing any value.Just pass the value bs which you retrieve, like this:

publishProgress(bs);

and update the progress bar with the balue of bs

Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43