1

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);
        }
    }

}
Dave
  • 297
  • 3
  • 4
  • 15

1 Answers1

0

You can try with this code snippet:

public boolean onKeyDown(int keyCode, KeyEvent event) { 
   if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { 
       // On Volume DOWN
       return true;
   }
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
       // On Volume UP
       return true;
}
else {
       return super.onKeyDown(keyCode, event); 
}

This way you can be sure that those buttons were pressed. If the problem persists, it's probably something not related to the button presses. }

g00dy
  • 6,752
  • 2
  • 30
  • 43
  • Thanks.. i have tried this, my class extends ContentObserver, these methods are undefined.. cant use them. – Dave Jul 01 '13 at 13:03
  • Ok, then how is `initialVolume` obtained and can the `delta` variable be null at some point? – g00dy Jul 01 '13 at 13:12
  • AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); initialVolume = audio.getStreamVolume(AudioManager.STREAM_RING); – Dave Jul 01 '13 at 13:16