Reputation: 2621
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);
}
Upvotes: 0
Views: 89
Reputation: 43023
You declared your 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.
Upvotes: 3