How to run the same asintet more than once?

I start asyncTask on the first start, then if the network connection is not available, I have an update button that tries to start asyncTask to try again. But I get a debugging error by talking about this.

07-29 18:14:21.290: ERROR/AndroidRuntime(9080): FATAL EXCEPTION: main 07-29 18:14:21.290: ERROR/AndroidRuntime(9080): java.lang.IllegalStateException: Cannot execute task: the task has already been executed (a task can be executed only once) 07-29 18:14:21.290: ERROR/AndroidRuntime(9080): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:541) 07-29 18:14:21.290: ERROR/AndroidRuntime(9080): at android.os.AsyncTask.execute(AsyncTask.java:499) 07-29 18:14:21.290: ERROR/AndroidRuntime(9080): at com.fttech.gameIT.MainMenu$1.onClick(MainMenu.java:90) 

Is it possible to run this twice?

+50
android
Jul 29 '11 at 22:16
source share
11 answers

Just create another instance and execute it.

+74
Jul 29 '11 at 22:20
source share

Like threads, AsyncTask cannot be reused. You must create a new instance every time you want to start it.

+34
Jul 29 2018-11-23T00:
source share

You can never execute the thread again, not in Java , and not in any other language, once the thread is executed using the run() method, it cannot be restarted, so you get an IllegalStateException ,

However, you can still call methods on this thread, but they will execute on the thread that calls them NOT in another thread. Therefore, you will need to create a new one.

+17
Jul 29 '11 at 10:22
source share

You cannot run the same AsyncTask instance more than once. Suppose you have an AsyncTask named MyAsyncTaks, and you intend to do something like this,

  MyAsyncTask myAsyncTask = new MyAsyncTaks(); myAsyncTask.execute(); // Works as expected . . . . myAsyncTask.execute(); // This will throw you exception 

The reason for this is that a thread that once completed its run method cannot be assigned to another task. Here, when you execute execute () for the first time, your AsyncTask started working and after completing its work the thread crashes. Naturally, the next call to execute () will throw you an exception.

The easiest way to run this more than once is to create a new instance of MyAsyncTaks and execute it.

  MyAsyncTask myAsyncTask = new MyAsyncTaks(); myAsyncTask.execute(); // Works as expected . . . MyAsyncTask myAsyncTask2 = new MyAsyncTaks(); myAsyncTask2.execute(); // Works as expected 

Although you do not need to mention it here, you need to remember that after the Android SDK version of Honeycomb, if you run several Asynchronous tasks at once, they will actually start sequentially. If you want to run them in parallel, use executeOnExecutor instead.

+6
Jan 29 '17 at 8:41
source share

Just create a new call like the new asyncTask (). execute (); You must create a new object to restart this task.

+4
Jul 29 2018-11-22T00:
source share

I just create an asynctask and then create a runnable that creates new asynctask instances. Then you can forward your runnable to the handler again and again.

 class MyAsyncTask extends AsyncTask<String, Void, String>{ ...} Runnable myRunner = new Runnable(){ public void run() { new MyAsyncTask ().execute(...); }}; myHandler.post(myRunner); 
+3
Jul 29 2018-11-22T00:
source share

I created an Arraylist of type ProgressUpdater (the name of the class that extends AsyncTask) and added instances to it (in the onClick button). Thus, you can perform and cancel this task if necessary.

 public class MainActivity extends Activity { ProgressBar progress; ProgressUpdater task; ArrayList<ProgressUpdater> pu = new ArrayList<MainActivity.ProgressUpdater>(); int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progress = (ProgressBar) findViewById(R.id.progress); } public void onClick(View v) { switch (v.getId()) { case R.id.btn: task = new ProgressUpdater(); pu.add(task); count++; pu.get(count - 1).execute(0); System.out.println("task" + task); // task.execute(10); break; case R.id.btnCancel: if (count >= 0) { pu.get(count - 1).cancel(true); pu.remove(count - 1); count--; } // task.cancel(true); break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private class ProgressUpdater extends AsyncTask<Integer, Integer, Void> { @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); progress = (ProgressBar) findViewById(R.id.progress); progress.setMax(100); } @Override protected Void doInBackground(Integer... params) { // TODO Auto-generated method stub int start = params[0]; for (int i = start; i <= 100; i++) { try { boolean cancelled = isCancelled(); if (!cancelled) { publishProgress(i); SystemClock.sleep(100); } } catch (Exception e) { e.printStackTrace(); } } return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); Log.v("Progress", "Finished"); } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); progress.setMax(0); } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); progress.setProgress(values[0]); } } 

}

0
Aug 21 '15 at 10:06
source share

in your MainActivity you can do this:

 LeoAsyncTask leoAsyncTaskGeneric; public void onClick_AsyncTask(View view) { LeoAsyncTask leoAsyncTaskInner = new LeoAsyncTask(); leoAsyncTaskInner.execute(); leoAsyncTaskGeneric=leoAsyncTaskInner; 

}

/ **, if you create space in the memory of your AsyncTask class as a shared one, then you can create an instance of the same class in the onClick method, and there are equal, so every time you click onClick, you will be using a new instance of the AsyncTask class, he will not give you problems * /

0
Dec 12 '17 at 1:01
source share

You can cancel the asynchronous task by pressing the button and then execute it again.

Inside the OnClic method:

 asyncTask.cancel(); AsyncTask asyncTask = new AsyncTask(); asyncTask.execute(); 
0
May 31 '18 at 15:53
source share

This solved my problem:

 public class MainActivity extends AnimationActivity { MyAsyncTasks asyncTasks = new MyAsyncTasks(); @BindView(R.id.refresh_btn) Button refreshBtn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUnbinder(ButterKnife.bind(this)); // ButterKnife usage syncTasks(); // run asyncTasks on activity start refreshBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { syncTasks(); // run asyncTasks on button click } }); } private void syncTasks() { try { if (asyncTasks.getStatus() != AsyncTask.Status.RUNNING){ // check if asyncTasks is running asyncTasks.cancel(true); // asyncTasks not running => cancel it asyncTasks = new MyAsyncTasks(); // reset task asyncTasks.execute(); // execute new task (the same task) } } catch (Exception e) { e.printStackTrace(); Log.e("MainActivity_TSK", "Error: "+e.toString()); } } } 
0
Apr 08 '19 at 16:56
source share

Flow rules

  • The AsyncTask class must be loaded into the user interface thread. This is done automatically with JELLY_BEAN.
  • A task instance must be created in the user interface thread. execute (Params ...) must be called in the user interface thread.
  • Do not call onPreExecute (), onPostExecute (Result), doInBackground (Params ...), onProgressUpdate (Progress ...) manually.
  • A task can be performed only once (an exception will be thrown if a second attempt is made).

For more information, please enter this link.

-one
Mar 07 '18 at 8:41
source share