amir secretery
amir secretery

Reputation: 203

Use asynctask to call a function in android

As below code shows I have a function named record() I want to call this function with asynctask but I do not know how to work with asynctask, record function takes long to do some tasks so I need to use asynktsak.

public class Record extends Activity {

    MediaPlayer mp;
    String name;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.record);

    mp = new MediaPlayer();

    record();

}

public void record() {
    .
    .
    .
  }
}

Upvotes: 2

Views: 11669

Answers (3)

user8938
user8938

Reputation: 559

Make AsyncTask Class and write code there and call your AsyncTask Class by new AsyncTaskClass().execute();

Upvotes: 1

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

Try this-

 public class Record extends Activity {

        MediaPlayer mp;
        String name;

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.record);
        mp = new MediaPlayer();
        new Task1().execute();  

    }


class Task1 extends AsyncTask<Void, Void, String> {

            @Override
            protected void onPreExecute() 
            {
            super.onPreExecute();
            }
            @Override
            protected String doInBackground(Void... arg0)
            {
               //Record method 
            }

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

            }
        }
    }

Upvotes: 3

Pratik Dasa
Pratik Dasa

Reputation: 7439

Try below code for your reference.

public void record() {

   new AsyncTask<Void, Void, Void() {

@Override
            protected void doInBackground(Void... params) {
}
Override
            protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

            }


}.execute();
  }

Upvotes: 0

Related Questions