Working on an app in android I have used Asynctask class, Works fine when i tested on my Android device running on 2.3.5, but the problem i am facing is, same is not working for my tablet 4.0.4
While testing, got to know that prexecute() is being called but doInbackground() not being called, however doInbackground() is being called on device(2.3.5).
One of the reason i believe for the problem is that the processor of Tablet is much faster than that of device, so may be some threading issues, dats why, to tackle this, i have used some flags, and used Thread.sleep() in a do while loop so that when condition is true, it works, but no luck, i am 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;
}
}