Is it possible start a new thread within an Async task?
Something like this:
public class FirstActivity extends Activity {
protected ProgressBar progBar;
protected Intent intent;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progBar = (ProgressBar)findViewById(R.id.start_progressBar);
progBar.setProgress(0);
new StartingApp().execute();
}
protected class StartingApp extends AsyncTask<Void, Integer, Void> {
int myProgress;
@Override
protected void onPreExecute() {
myProgress = 0;
}
@Override
protected Void doInBackground(Void... params) {
while(myProgress<50){
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(10);
}
MyRunnableClass mrc = new MyRunnableClass();
mrc.run();
return null;
}
@Override
protected void onPostExecute(Void result){
intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
}
@Override
protected void onProgressUpdate(Integer... values) {
progBar.setProgress(values[0]);
}
}
}
MyRunnableClass is a class which implements Runnable.
I want something like this because in the first activity I want to show a progress bar while the application is initializing (fill data structures, starting threads).
Another question I have is: should I use the run() or start() method?
Thanks in advance!
Why do you want to do that?
As pointed out in the code you need to call new Thread(mrc).start() to make it work. Otherwise i dont see any problem in that code spawning a new thread.