Question

I'm trying to implement simple app which shows ProgressBar updating. Following are my main tasks.

  • When app load first time and start ProgressBar uploading using AsyncTask it should display on UI (Done and working properly)

  • While updating ProgressBar, user exit from the app, uploading value should increment caz it is run on the AsyncTask (Done and working properly)

  • Next time app loading, ProgressBar should be start from the current updating progress value if there is still updating and running AsyncTasks (Issue)

Here is part of my code;

public class PUMainActivity extends Activity implements ProgUpAsync.MyThreadListener {

    private ProgressBar progressBar;

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

        progressBar = (ProgressBar) findViewById(R.id.pumainActivity_progressBar);
    }

    @Override
    public void onTextViewChanging(final int progValue) {

        runOnUiThread(new Runnable() {

            @Override
            public void run() {

                progressBar.setProgress(progValue);
            }
        });
    }
}

Because of the listener I've implemented, I can get the current progress even second time when my app loads. But the ProgressBar UI is not updating. No compile/run time errors.

UPDATE with listener interface


Here is the implementation of listener

public class ProgUpAsync extends AsyncTask<Void, Integer, Void> {

    private MyThreadListener mCallback;

    public interface MyThreadListener {
        public void onTextViewChanging(int progValue);
    }

    public ProgUpAsync(Activity paramActivity) {

        try{
            mCallback = (MyThreadListener) paramActivity;
        }catch(ClassCastException e){
            throw new ClassCastException(paramActivity.toString() + " must implement MyThreadListener");
        }
    }

    @Override
    protected Void doInBackground(Void... params) {

        for(int i=0; i<=100; i++){
            if((i%5) == 0){
                publishProgress(i);

                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ie) {
                }
            }
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

        mCallback.onTextViewChanging(values[0]);
    }
}
Was it helpful?

Solution 2

Finally I did it. Here is the code I've added to get the task done.

In ProgUpAsync.java

public void setMainActivity(Activity paramActivity){

    try{
        mCallback = (MyThreadListener) paramActivity;
    }catch(ClassCastException e){
        throw new ClassCastException(paramActivity.toString() + " must implement MyThreadListener");
    }
}

And in PUMainActivity.java

    try{
        if(progUpAsync.getStatus() == AsyncTask.Status.RUNNING){
            if(PuConstants.debugEnable)Log.d(TAG, "# Async is RUNNING");
            progUpAsync.setMainActivity(this);
        }else{
            if(PuConstants.debugEnable)Log.d(TAG, "# Async is NOT running");
            progUpAsync = new ProgUpAsync(this);
            progUpAsync.execute();
        }
    }catch(NullPointerException npe){
        if(PuConstants.debugEnable)Log.w(TAG, "@ Exception caz of progUpAsync : " + npe);
        progUpAsync = new ProgUpAsync(this);
        progUpAsync.execute();
    }

OTHER TIPS

The problem is you can't retrieve reference to your ProgUpAsync after the application is rebooted. Maybe you have to see to translate this in a Service. Which is do to do this kind of update process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top