I am trying to avoid executing duplicate tasks on a Service by using a synchronised Map in the onStartMethod and then checking that a key is not already stored. However, so far is not working, it is executing the same thing twice if I call start the service twice soon enough.
public void onCreate() {
SYNCED_TABLES = Collections.synchronizedMap(new Hashtable<>());
}
public int onStartCommand(Intent intent, int flags, int startId) {
synchronized (SYNCED_TABLES){
if(!SYNCED_TABLES.containsKey(intent.getStringExtra(KEY))){
SYNCED_TABLES.put(intent.getStringExtra(KEY), true);
/* Do stuff on a Handler thread */
}
else{
Log.d(TAG, "Tried to execute the same task twice " + intent.getStringExtra(KEY));
}
}
}