// let’s in the most important, the Timer class has a method called Schedule (Schedule), /where this method receives the (Timertask,long delay, long timesinmillis);
public void inicializaTimer() {
this.mCalendarDateFirst.setTimeInMillis(mTime);
this.mCalendarDateFirst.add(Calendar.HOUR, -1);
String data = this.mCalendarDateFirst.getTime().toString();
this.startaThread();
this.mTimer.schedule(this.mTimerTask, mCalendarDateFirst.getTime(), 1000);
}
public void startaThread() {
this.mTimerTask = new TimerTask() {
@Override
public void run() {
//Today on android using the timetask run and Handle’s method the post.
if you want to know more consultation in the documentation of android "processes and chaining".
mHandler.post(new Runnable() {
@Override
public void run() {
if (stop) {
mContext.stopService(new Intent(mContext, ServicoNotificacaoBackGround.class));
} else {
mCalendarAtual.setTimeInMillis(System.currentTimeMillis());
mCalendarHorario.setTimeInMillis(mTime);
Log.i("Data String", "Time " + mCalendarHorario.getTime().toString());
Log.i("Data String 2", "Time " + mCalendarAtual.getTime().toString());
if (mCalendarHorario.getTime().toString().equalsIgnoreCase(mCalendarAtual.getTime().toString())) {
stop = true;
}
Toast.makeText(ExampleService.this, "Foi ! ", Toast.LENGTH_SHORT).show();
Log.d("ExampleService", "Serviço Executando com Sucesso");
}
}
});
}
};
}
this is an example that could be the solution to the problem, i.e., running every 1 second.
What error does it make? What code is running within the method
run()
.– ramaral