i am developing an android app, where i want to start a timer upon pressing the volume down button, and stop the timer upon pressing volume up button, and restart the timer upon volume down again and so on.
The problem is, the time isnt getting stopped even after i purge and cancel it. Its looping itself continuously.
Please check the code below and let me what possibly could be wrong.
@Override
public void onChange(boolean selfChange)
{
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int delta=initialVolume-currentVolume;
//if down pressed
if(delta>0)
{
Log.e("delta","down");
downpress = "yes";
downpressdonefirsttime = "yes";
timer = new Timer();
}
//if up pressed
else if(delta < 0)
{
Log.e("delta","up");
}
//if down + up pressed
else if(delta == 0)
{
if(downpress.equalsIgnoreCase("yes"))
{
Log.e("inisde","delta is 0 , shake it!!");
}
}
if(downpressdonefirsttime.equalsIgnoreCase("yes"))
{
if(downpress.equalsIgnoreCase("yes"))
{
timer.scheduleAtFixedRate(new TimerTask()
{
@Override
public void run()
{
time = ++time;
Log.e("inside","time run , time is" + time);
if(time <= 5)
{
Log.e("inside","time less than 5");
downpressdonefirsttime = "no";
}
if(time > 5)
{
Log.e("inside","time greater than 5");
downpress = "no";
time = 0;
timer.cancel();
timer.purge();
}
}
}, 0, 1000);
}
}
}