我正在尝试编写一个简单的服务,每隔几秒钟就会做一些事情。但是当我尝试在主 Activity 中运行其他 AsyncTasks 时运行服务时,我注意到其他 AsyncTasks 卡在 onPreExecute 上。当我禁用该服务时,一切都按预期工作。服务的 AsynTask 阻塞其他 AsyncTask 是否有原因?
服务
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 的 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;
}