I have a background service to be called everyday at 1pm. At 1 pm, 3 values in my Firebase database should be set to 0. When the user launches a certain activity the background service is triggered. The values reset to 0 at 1pm like i want but, every time after 1pm when the activity is launched by the user the service runs resetting my values. How can I get the values reset to 0 at 1pm and not again until 1pm the next day?

Below is my code within the Service class

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 13);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);

    timer = new Timer();
    timer.schedule(new TimerTask()
    {
        @Override
        public void run()
        {
            String id = FirebaseAuth.getInstance().getCurrentUser().getUid();

            FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("steps").setValue(0);
            FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("bonusScore").setValue(0);
            FirebaseDatabase.getInstance().getReference().child("Users").child(id).child("stepsScore").setValue(0);
        }
    }, calendar.getTime(), TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS));

    return START_STICKY;
}
@Override
public void onDestroy()
{
    super.onDestroy();
}

This is the Code starting the service. This method is called in the oncreate method of this activity.

private void startBackgroundService()
{
    startService(new Intent(this, BackgroundService.class));
}

Related posts

Recent Viewed