Post new topic in Async Task

Is it possible to start a new thread in an Async task? Something like that:

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 that implements Runnable. I want something like this, because in the first step I want to show a progress bar when the application is initialized (fill in data structures, initial threads).

One more question: should I use the run () or start () method?

Thanks in advance!

+4
source share
1 answer

Why would you want to do that? As indicated in the code, you need to call the new Thread (mrc) .start () to make it work. Otherwise, I do not see any problems in this code, creating a new thread.

0
source

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


All Articles