I'm trying to write a simple Service that will do something every few seconds. But when I run the Service while trying to run other AsyncTasks in the main Activity, I noticed that the other AsyncTasks get stuck at onPreExecute. When I disabled the service, everything worked as expected. Is there a reason why the Service's AsynTask is blocking the other AsyncTasks?
Service
public class SVC_SyncData extends Service {
private final String TAG = "SVC_SyncData";
private AT_SyncData m_SyncPoll = null;
public static boolean isRunning = false;
public SVC_SyncData() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
m_SyncPoll = new AT_SyncData(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
m_SyncPoll.execute();
isRunning = true;
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy");
m_SyncPoll.cancel(false);
isRunning = false;
}
}
AT_SyncData's doInBackground
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
try {
while (!isCancelled()) {
Log.i(TAG, "syncData");
// Sleep until next cycle
Thread.sleep(5000);
}
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}