Show big task result after ProgressDialog


I have an application that performs a long task and returns a value. During task a ProgressDialogshows progress. After completing the task, I want to show the result in TextView. I am running a task in FutureTask.
My problem is that if I try to get the result, the .get()FutureTask method blocks the UI thread and I don't see ProgressDialog( TextViewdisplays the result properly).

My code for the task (pool - ExecutorService):

final FutureTask<String> future = new FutureTask<String>(new Callable<String>() {  
    @Override  
    public String call() {  
        return myLongTask();  
    }  
});  
pool.execute(future);

And after that I call updateProgressBar()in Runnable, which updates ProgressDialogwith Handler:

Runnable pb = new Runnable() {  
    public void run() {  
        myUpdateProgressBar();  
    }  
};  
pool.execute(pb);

, , ProgressDialog:

String result = future.get()

updateProgressBar() ( ) ProgressDialog, :

, , .

, ? ( AsyncTasks, , .)

+3
2

, AsyncTask Thread/Handler, .

, , , . , .

+2

, ExecutorService, :

String result = future.get()

, AsyncTask future.get(), , :

private class FutureTask extends AsyncTask<Future<String>, Void, String>{

    @Override
    protected PhotoToView doInBackground(Future<String>... params) {
        Future<String> f = params[0];
        try {
            return f.get(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String futureResult) {
        super.onPostExecute(futureResult);

        // this method is run on UI thread
        // do something with the result 
        // or simply hide the progress bar 
        // if you had previously shown one.

    }
}

:

FutureTask ft = new FutureTask();
ft.execute(future);

, .

0

Source: https://habr.com/ru/post/1790086/


All Articles