Rebooting Android AsyncTask

I am having trouble restarting AsyncTask after trying to reopen the activity.

When I first open the action, I call it to launch AsyncTask , which works for the first time.

 myTask connectedTask; connectedTask = new myTask(); connectedTask.execute(); public class myTask extends AsyncTask<Integer,Integer, Integer> { @Override protected Integer doInBackground(Integer... arg0) { //Increase timer and wait here in a loop System.out.println("ASYNC TASK STARTING!"); return IsSocketConnected(); } protected void onPostExecute(Integer result) { //Something you want to do when done? System.out.println("ASYNC TASK DONE!"); // if it was connected successfully if(result == 1) { // remove the progress bar proBar.setVisibility(View.GONE); // Discover available devices settings and create buttons CreateButtons(btnList); } } } IsSocketConnected(); // checks for a bluetooth connections to be done. 

When I go back to the previous action and try to start this operation again, I cannot start AsyncTask to start again.

I read that while I create a new instance of AsyncTask , I have to restart these tasks.

Is there anything else I should do?

Thanks,

+6
source share
7 answers

Thanks for all your comments, they really helped. I decided to do away with AsyncTask. I ended up using a regular executable thread and used handlers to send messages back to the user interface thread. here is the code:

  // Start thread here only for IsSocketConnected new Thread(new Runnable() { public void run() { //Add your code here.. IsSocketConnected(); } }).start(); // handler that deals with updating UI public Handler myUIHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == Bluetooth.STATE_CONNECTED) { //Update UI here... Log.d(TAG, "Connected"); // Discover available devices settings and create buttons CreateButtons(btnList); } else if(msg.what == Bluetooth.STATE_NONE) { Log.d(TAG, "NOT Connected"); } 

}

 // in the IsSocketConnected() I call this Message theMessage = myUIHandler.obtainMessage(Bluetooth.STATE_CONNECTED); myUIHandler.sendMessage(theMessage);//Sends the message to the UI handler. 

It still works. Thanks again. Hope this helps someone.

+2
source

That's right, you can just create a new instance and run it to get started. I wonder what function you are running? It would probably be nice to run it in your onResume() function, just in case the action is not destroyed before it comes to the fore.

+2
source

This can probably help someone who comes across this in the future. You can simply add a method:

 public class MainActivity extends Activity { public static MyTask loadTextDataTask; //... methods public class MyTask extends AsyncTask<Void, Void, Void> { //... methods public void selfRestart() { loadTextDataTask = new MyTask(); } } 

}

Then you can use it from any other class, for example:

 MainActivity.loadTextDataTask.selfrestart(); 
+2
source

Try entering this code in onResume() , for example:

 @Override public void onResume() { super.onResume(); myTask connectedTask = new myTask(); connectedTask.execute(); } 

It should create a new object when you return to your Activity . You did not publish it, but you probably created an object in onCreate() , which runs only once.

My recommendation for handling Socket connections is just a subclass of Thread .

0
source

It is better to use OnPostExecute to start a new instance of the task. It will work if the task is completed. But use a boolean value to prevent it from starting every time

 protected void onPostExecute(Void result) { if(someboolean==true) { new instance of task; instance.execute; someboolean=false; } } 
0
source

Developer website says

At the first input, AsyncTasks were executed sequentially on one background thread. Starting with DONUT, this has been changed to a thread pool, allowing multiple tasks to run in parallel. Starting with HONEYCOMB, tasks are executed in a single thread to avoid common application errors caused by parallel execution.

If you really need parallel execution, you can call executeOnExecutor (java.util.concurrent.Executor, Object []) with THREAD_POOL_EXECUTOR.

Starting with HONEYCOMB, AsyncTask uses one default Executor to perform tasks, which then runs one task at a time. This means that if you call myTask.execute () after you have already called it with any action ( anywhere in the main thread ), the second call will wait to call the doInBackground method until the first task completes, if your method is IsSocketConnected ( ) does not return immediately, it blocks any calls to doInBackground () after calling the AsyncTask.execute () method again anywhere in the main thread until the current Task completes.

If your isSocketConnected () uses any locking operations, such as wait () or Thread.sleep (), you can bypass it by calling myTask.cancel (true) in the onDestroy () callback to your activity, which will throw an InterruptedException. Just make sure you catch InterruptedException in a place that will cause the thread to exit the doInBackground method.

You can also make a workaround using a custom ThreadPoolExecutor and calling the AsyncTask.executeOnExecutor (Executor) method.

0
source

Instead of using: connectedTask = new myTask (); connectedTask.execute ();

Just use: newconnectTask.execute (); Use it where u definitely wants to restart the task!

0
source

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


All Articles