0

Hi every one
I'm new in android I'm trying to save a binary file with Asynctask and show process in a TextView

This is the layout:

        <TextView
        android:id="@+id/textView_pecent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

And this is my java code:

    private class hideit extends AsyncTask<String, Integer, Boolean> {
    @Override
    protected Boolean doInBackground(String... urls) {
        try{

            int c;
            while((c = Data.read()) != -1) {
                Data.write(c);
            }

        }catch (IOException e){
            e.printStackTrace();
        }

        return true;
    }

    protected void onProgressUpdate(Integer... progress) {
      textview.setText(INTEGER.toStirng(x)); //x =0
      x++
    }

    protected void onPostExecute(Boolean result) {

        Toast.makeText(getActivity(),"Done", Toast.LENGTH_LONG).show();
    }
 }

How do i use publish process? I searched a lot but everyone use sleep or for or while loop but you can't do your process with sleep
I'm so confused how can i use this function to show my process in TextView?

Should i call it many times?
Thanks

edit: This is an example:

  protected String doInBackground(Integer... params) {
        for (; count <= params[0]; count++) {
            try {
                Thread.sleep(1000);
                publishProgress(count);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "Task Completed.";
    }

How can i add below code to the example

            try{

            int c;
            while((c = Data.read()) != -1) {
                Data.write(c);
            }

        }catch (IOException e){
            e.printStackTrace();
        }

        return true;
    }
Khashayar
  • 3
  • 3
  • There are already a few answers for how to use a progress updater. See: https://stackoverflow.com/a/18069882/1531971 If this does not address your issue, you need to say why. –  Jul 05 '18 at 17:15
  • Thanks it says //Copy you logic to calculate progress and call what that means? . And should i call function many times in my process – Khashayar Jul 05 '18 at 17:20
  • Your background process updates the progress based on your calculations/estimations. See: https://www.concretepage.com/android/android-asynctask-example-with-progress-bar –  Jul 05 '18 at 17:26
  • Thanks for reply but one important thing i add something to my question please give me some advice – Khashayar Jul 05 '18 at 18:32
  • Your `doInBackground` task should be doing the actual work you want done off the main thread. As part of this work it should decide to invoke `publishProcess` one or more times as the number of bytes you have written increases. There are many, many, many examples and tutorials on how to do this. –  Jul 05 '18 at 18:41
  • Thanks again could you give me an example ? How can i calculate the process and call it ? – Khashayar Jul 05 '18 at 19:33
  • Well, remember that your Streams have methods that take buffers and return integers. You can use this to keep track of progress. How about using a buffer of a known size, like 2048, and each time one or more buffers are written you update your progress. –  Jul 05 '18 at 20:07

0 Answers0