ChyBy
ChyBy

Reputation: 1685

Asynctask out a UI thread

I'd like to brand the AsyncTask class for doing something in background updating the caller thread with the progress but with the caller thread != of UI thread. I've tried that code but the line publishProgress(i) seems have not effect. Can someone tell me how can I fix it (or what other class can I use else). Thanks in advance =)

 public class MainUI extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Thread t=new Thread(){
                    boolean exit=false;
                    public void run(){
                        Looper.prepare();
                         new DownloadFilesTask().execute();

                         while (!exit){
                             try {
                                Thread.sleep(600);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                         }

                         Log.d("","Exit thread");

                        Looper.loop();
                    }

                    public void exit(){
                        exit=true;
                    }

                    class DownloadFilesTask extends AsyncTask<Void, Long, Long> {
                        protected Long doInBackground(Void... urls) {
                            long i=0;
                            for (i=0;i<20;i++){
                                 Log.d("",i+" ");
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                publishProgress(i);
                                }
                            return i;
                        }
                        protected void onProgressUpdate(Long... progress) {
                            Log.d("Test",progress[0]+"");
                                        }

                                        protected void onPostExecute(Long result) {
                                            exit();
                                        }
                                    }


                                };
                                t.start();


                            }

                        });
                    }





}

Upvotes: 0

Views: 1919

Answers (2)

matsjoe
matsjoe

Reputation: 1480

Creating a thread to start an ASyncTask is useless and should not be done. You should read the painless threading document on the android developer site

http://android-developers.blogspot.com/2009/05/painless-threading.html

Upvotes: 1

Nabil Gardon
Nabil Gardon

Reputation: 175

From the Android Doc :

There are a few threading rules that must be followed for this class to work properly:

The task instance must be created on the UI thread.

execute(Params...) must be invoked on the UI thread.
Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
The task can be executed only once (an exception will be thrown if a second execution is attempted.)

So you can NOT create it outside of the UI Thread. Use Task instead and wrap it in a ThreadPoolExecutor object. Be aware that you need to make it thread-safe when updating the UI by using one of those :

Activity.runOnUiThread(Runnable)
View.post(Runnable)
View.postDelayed(Runnable, long)

But once again, Asynctask is useless and I do NOT recommend it.

Regards

Upvotes: 0

Related Questions