SOLUTION
Instead Services, I've used AlarmManager to solve the problem, and now, it works properly.
Thanks all!
I'm trying to use a service multiple times with different information.
An example of the code for the service that I've written is this:
public int onStartCommand(Intent intent, int flags, int idArranque) {
int time = intent.getExtras().getInt("timePhase");
new CountDownTimer(time*1000,5000){
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
//Other code here....
Toast.makeText(getApplicationContext(),"Finished: "+nameMedicine,Toast.LENGTH_LONG).show();
}
}.start();
return START_STICKY;
}
From the Activity, each time that I call the Service, I pass a bundle with the time for the CountDown, but it not works correctly.. I'm assuming that the variables that are received in the service are overwritten by the last call to the service.
Is there any way to run each service in a different process to avoid the overwriting?
I've read this:
Link 1: Use same service multiple times
Link 2: What happens if a Service is started multiple times?
Link 3: Multiple timers using same service
But I don't know exactly what should I do.
UPDATE: DETAILED INFORMATION
Service.java
public class timePhase extends Service {
SQLiteDatabase db;
UsuariosSQLiteHelper usdbh;
String nameMedicine;
int time=0;
int idPaciente;
int idMedicamento;
int count=0;
Bundle b;
@Override
public void onCreate() {
}
@Override
public int onStartCommand(Intent intent, int flags, int idArranque) {
count++;
int time = intent.getExtras().getInt("timePhase");
idPaciente = intent.getExtras().getInt("idPatient");
idMedicamento = intent.getExtras().getInt("idMedicine");
banAccess(true);
new CountDownTimer(time*1000,5000){
//TODO: Toast no funciona
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
banAccess(false);
Toast.makeText(getApplicationContext(),"Ha acabado el tiempo de espera de "+nameMedicine,Toast.LENGTH_LONG).show();
count--;
if(count==0) stopSelf();
}
}.start();
return START_STICKY;
}
@Override
public void onDestroy() {
}
@Override
public IBinder onBind(Intent intencion) {
return null;
}
public void banAccess(boolean status){
usdbh = new UsuariosSQLiteHelper(this, "DB", null, 1);
usdbh.getWritableDatabase();
db = usdbh.getWritableDatabase();
if(status) db.execSQL("Update pacienteCompuesto set validado=4 where id_paciente="+idPaciente+" and id_medicamento="+idMedicamento);
else db.execSQL("Update pacienteCompuesto set validado=2 where id_paciente="+idPaciente+" and id_medicamento="+idMedicamento);
}
}
Each time that I click on a item on a ListView from an Activity, this service is launched. I pass some variables to the service with a click on a item from the ListView through a Bundle, like the timer to the CountDown, and other important variables to alter the database on the finish of the CountDown.
If I click on one item of the ListView and the service is launched, it works ok. If I click on one item of the ListView, and then I click on other item, the local variables of the service are of the second item... By this, the CountDown for the first item clicked not finish correctly (the instruction to modify the database is not executed with the values of the first item clicked, but yes with the values of the second clicked item).