Asynctask DoInBackground () not called on Android Tablet

Working on an application in android I used the Asynctask class, it works great when I tested it on my Android device running on 2.3.5, but the problem I encountered also does not work for my tablet 4.0.4

During testing, find out that prexecute () is being called, but doInbackground () is not being called, however, doInbackground () is being called on the device (2.3.5).

One of the reasons I believe in the problem is because the Tablet processor is much faster than the processor, so there may be some thread problems, for example, why, to solve this problem, I used some flags and used Thread. sleep () in the do while loop, so when the condition is true, it works, but with no luck, I'm stuck in the loop itself. Here is my code:

MyAsyncTask object = new MyAsyncTask (MainActivity.this); runOnUiThread(new Runnable() { public void run() { try { if (object.isReady() || !object.isStarting()) { return; } object.execute(); do { Thread.sleep(1000); } while (!object.isReady() && object.isStarting()); if(!object.isReady()) { return; } } catch (InterruptedException e) { e.printStackTrace(); } } }); 

AsynctaskClass:

 public class MyAsyncTask extends AsyncTask<Void, Void, Boolean>{ private ProgressDialog dialog; private Context context; private boolean isStarting = false; private boolean isReady = false; public AsyncUpdatesofJquery(Context context) { this.context = context; isStarting = true; isReady = false; } public boolean isStarting() { return isStarting; } public boolean isReady() { return isReady; } @Override protected void onPreExecute() { isStarting = true; isReady = false; dialog = new ProgressDialog(context); dialog.setMessage("Downloading Files, Please wait..."); dialog.show(); } @Override protected Boolean doInBackground(Void... params) { isReady = true; isStarting = false; downloadFiles(context); // my background task return true; } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); context.startActivity(new Intent(context, NewActivity.class)); dialog.dismiss(); isReady = false; isStarting = false; } } 
+6
source share
1 answer

A multi-thread model has changed between 2.3.5 and 4.0.4. AsyncTask now by default has all subclasses in the application using the same thread (i.e. only one AsyncTask can be run at a time!). He explained here :

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.

With this in mind, it may be that another AsyncTask running in your application, thereby preventing it from starting. This explains why it works great on your 2.3.5 device, but not on your 4.0.4 tablet.

+17
source

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


All Articles