3

Ok, here is my problem. I want to learn AsyncTask, Threading and Handler to process long running tasks. I used Android Cook Book and New Boston Android tutorial, but I can't make it work.

I need to change the progress bar message. This is not for project, but to learn threading. Threading is really hard for me. So if you have any tutorial or guide to understand threading please let me know.

Meanwhile I tried Handler class, but it didn't work. So I tried AsyncTask. But it does not work either.

Here is my code.

package com.example.androidpopup;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button click_btn;
    ProgressDialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        click_btn = (Button) findViewById(R.id.button1);

        click_btn.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                dialog = ProgressDialog.show(MainActivity.this, "",
                        "Recording...", true);
                dialog.setCancelable(true);
                Thread thread = new Thread(){
                    public void run(){
                        try{
                            sleep(2000);
                            new UpdateUI().execute("testing");
                        }catch(InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                };
            }
        });
    }

    public class UpdateUI extends AsyncTask<String, Integer, String>{

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return "Searching";
        }

        protected void onPreExecute(String f){
            //example of setting up something
            f = "whatever";
        }

        protected void onProgressUpdate(Integer...progress){

        }

        protected void onPostExecute(String result){
            dialog.setMessage(result);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

If anyone can explain what's wrong of my approach it would be really helpful.

Thank you!

Isuru Madusanka
  • 1,397
  • 4
  • 19
  • 27
  • 2
    How doesn't it work...? Be more descriptive. – JoxTraex Aug 05 '12 at 04:53
  • It doesn't change the message. I have no idea how to inject debug codes either. – Isuru Madusanka Aug 05 '12 at 04:59
  • 1
    Should you define a class derived from Thread, (class MyThread extends Thread)? Should there not be a Thread.start() call somewhere? – Martin James Aug 05 '12 at 06:12
  • 1
    Also - debugging - does the first line of run() get executed? You should realize now that if you cannot debug, you cannot develop any software, never mind multithreaded apps. It's an absoultely fundamental skill you must acquire. – Martin James Aug 05 '12 at 06:17
  • 1
    You are starting your AsyncTask in your thread, but you never call start() on the thread itself, so it never runs. – Raghav Sood Aug 05 '12 at 06:42

1 Answers1

3

Why are you creating a new Thread just to wait 2 seconds to call AsyncTask? I would suggest you to remove that Thread, normally call the AsyncTask and inside it place Thread.sleep(....).

Something like:

protected void onPreExecute(String f){
     Thread.sleep(2000);
     //example of setting up something
     f = "whatever";
}

PS: in order to run a Thread, you need to call thread.start. ;)

Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66