0

I want to reset my horizontal progressbar to 0 every night at 12. Here is my code.

public class MainActivity extends AppCompatActivity {
     
     private TextView textView;
     Button btn; 
     private int Counter counter;
     Progressbar progressbar;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         textView = findViewById(R.id.txtvw);

         progressbar = findViewById(R.id.progressbar_Horizontal);
         progressbar.setMax(10);
                
         btn = findViewById(R.id.btn_clc);
         btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
              counter++;
              textView.setText(String.valueOf(counter));
              progressbar.setProgress(counter)
              }
         });            

         Calendar calendar = Calendar.getInstance();
         int hour = 23;
         int minute = 59;
         int second = 59;
    
         int curHour = calendar.get(Calendar.HOUR_OF_DAY);
         int curMinute = calendar.get(Calendar.MINUTE);
         int curSecond = calendar.get(Calendar.SECOND);
     
         if (hour==curHour && minute==curMinute && second==curSecond) {
            progressbar.setProgress(0);
         }
     }
}

In this method at the output there is no response to the progressbar! Is this the right way to do or is there any other way?

1 Answers1

0

You don't get any response because you're defining the action in OnCreate method. This method is executed one time when you enter the Activity. So this action defined only works if you launch your Ativity exactly at 23:59:59.

If you want to do it right, implement AlarmManager. You can set the time you want (23:59:59) and do whatever you want (reset the bar) at that moment. You can implement it also when the app is closed, in the background or when the screen is off.

Take a look at this: Alarm Manager Example