AsyncTask no funciona correctamente cuando se llama repetidamente desde un hilo
Frecuentes
Visto 87 veces
0
I am making an app which sends update to web server when a songs starts playing...I do the the web server update through asynctask however some times some information gets skipped and some infos are sent repeatedly. In some case asyntask remain in running state making it not executing after that. Main thread works good.
I declared the instance like this in MainActivity
private static AsyncTask<Void, Void, Void> mTask = null;
code for Asynctask is
private class SendingData extends AsyncTask<Void,Void,Void>{
@Override
protected void onPreExecute(){
//some task
}
protected void onPostExecute(Void params){
Log.d("Tesing","After Post");
super.onPostExecute(params);
}
@Override
protected Void doInBackground(Void... arg0) {
sendPost();
return null;
}
}
My call to asynctask when a new song changes in main thread. This is a repetitive call
if(mTask.getStatus() == AsyncTask.Status.FINISHED){
// My AsyncTask is done and onPostExecute was called
Log.d("AsyncTask Status","Finished");
mTask = new SendingData();
mTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}else if(mTask.getStatus() == AsyncTask.Status.PENDING){
Log.d("AsyncTask Status","Pending");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
1 Respuestas
3
Declaraste tu AsyncTask
as static variable. This means that your references will get mixed up, i.e. when you start a new AsyncTask
, you will overwrite the previous reference with a new one.
respondido 27 nov., 13:05
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas android multithreading android-asynctask or haz tu propia pregunta.
Shall I make a new reference each time when encounter or have to call asynctask? how would i check the status of the previously called asynctask or do i just call a new one without thinking about the previous one like below new SendingData().executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); - Saty
which is one is better AsyncTask.SERIAL_EXECUTOR or AsyncTask.THREAD_POOL_EXECUTOR for repetitive task? Please explain me. - Saty