1

I basically want a stopwatch activity in my app that will vibrate the device after 30 seconds have elapsed and sound a notification alert at 60 seconds. I am new to app development so please don't hound me for missing an obvious answer.

I know I need Chronometer.OnChronometerTickListener - but not sure how to implement?

public void shotClockStart(View v) {

        Chronometer shotclock = (Chronometer) findViewById(R.id.chrono1);
        shotclock.start();
        long timeElapsed = SystemClock.elapsedRealtime() - shotclock.getBase();
        if (timeElapsed >= 30000) {
            //  HERE I WANT A VIBRATION ON THE DEVICE.
        }else if(timeElapsed>=60000){
            //HERE I WANT A NOTIFICATION ALERT 
        }
    }
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257

1 Answers1

1

You have to set the listener on the Chronometer

public void shotClockStart(View v) {
    Chronometer shotclock = (Chronometer) findViewById(R.id.chrono1);
    shotclock.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
        @Override
        public void onChronometerTick(Chronometer chronometer) {
            long timeElapsed = SystemClock.elapsedRealtime() - chronometer.getBase();
            if (timeElapsed >= 30000) {
                //  HERE I WANT A VIBRATION ON THE DEVICE.
            }else if(timeElapsed>=60000){
                //HERE I WANT A NOTIFICATION ALERT
            }
        }
    });
    shotclock.setBase(SystemClock.elapsedRealtime());
    shotclock.start();
}
Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107