I have an activity that starts after a certain time. Once this activity starts, the user is supposed to press a button that starts a new timer.
However, if the user does not press the button I want a toast to be displayed every 5 minutes until the button is pressed.
I have a listener for the button:
startBreakButton = (Button) findViewById(R.id.startBreakButton);
startBreakButton.setOnClickListener(mStartListener);
I have the code for displaying the toast and everything else is working well.
I’m just not sure where to put the code I want to run before the button is clicked. Within the mStartListener code or somewhere else such as within the onCreate method?
UPDATE
public class BreakActivity extends Activity {
Button startBreakButton; // button to start the break timer
Boolean clicked= false;
CountDownTimer counter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakscreen); // sets layout to breakscreen.xml
MyCount counter;
counter=new MyCount(5000,1000);
counter.start();
startBreakButton = (Button) findViewById(R.id.startBreakButton);
startBreakButton.setOnClickListener(mStartListener);
View.OnClickListener mStartListener = new OnClickListener() {
clicked=true;
//other listener code
};
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
if(clicked==false){
Toast.makeText(getApplicationContext(), "TAKE A BREAK", Toast.LENGTH_LONG).show();
counter= new MyCount(5000,1000);
counter.start();
}
}
@Override
public void onTick(long millisUntilFinished) {
long s1 = millisUntilFinished;
}
}
};
EDIT – Above solution is not working, it needed to be clicked==false, not clicked=false!!
Try like this..
in oncreate create a acountdowntimer..
MyCount class..
and in onclicklistener of startBreakButton put