I am using the Timer and the TimerTask classes in one of my MIDlets to generate an action after 3 seconds.
Actually what I want to do is generate Action 1 if a button is pressed and released for less than 3 seconds and I want to generate Action 2 if a button is pressed and released for >= 3 seconds.
I amusing the methods keyPressed() and keyReleased () of the canvas class. When a key is pressed I make a new Timer instance and schedule a TimerTask to generate Action 2. But if the key is released i.e keyReleased is called I check if the scheduled task has already been done or not if not I cancel the Timer and the TimerTask and generate Action 1. The problem is cancelling the instances has no effect and the Action 2 is still generated if I momentarily press and release the button. The code listing of keyPressed and keyReleased is as follows:
protected void keyPressed(int keyCode)
{
switch (keyCode)
{
case KEY_NUM1:
Sec3ThreadT1 = new Timer();
threeSecT1 = new ThreeSecPressAction(true, this);
Sec3ThreadT1.schedule(threeSecT1, 3000);
break;
case KEY_NUM3:
Sec3ThreadT2 = new Timer();
threeSecT2 = new ThreeSecPressAction(false, this);
Sec3ThreadT2.schedule(threeSecT2, 3000);
break;
}
}
protected void keyReleased(int keyCode)
{
switch (keyCode)
{
case KEY_NUM1:
Sec3ThreadT1.cancel();
threeSecT1.cancel();
if(threeSecDoneT1)
{
//Action 2 Already done by Timer
threeSecDoneT1 = false;
}
else
{
//Generate Action 1
}
break;
case KEY_NUM3:
Sec3ThreadT2.cancel();
threeSecT2.cancel();
if(threeSecDoneT2)
{
//Action 2 Already done by Timer
threeSecDoneT2 = false;
}
else
{
//Generate Action 1
}
break;
}
}
Why is it when I press the button briefly I get Action 1 and Action 2 both. But when I keep the button pressed for 3 seconds I get only Action 2??
Please help me !!!
Actually what I want to do is generate Action 1 if a button is pressed and released for less than 3 seconds and I want to generate Action 2 if a button is pressed and released for >= 3 seconds.
I amusing the methods keyPressed() and keyReleased () of the canvas class. When a key is pressed I make a new Timer instance and schedule a TimerTask to generate Action 2. But if the key is released i.e keyReleased is called I check if the scheduled task has already been done or not if not I cancel the Timer and the TimerTask and generate Action 1. The problem is cancelling the instances has no effect and the Action 2 is still generated if I momentarily press and release the button. The code listing of keyPressed and keyReleased is as follows:
protected void keyPressed(int keyCode)
{
switch (keyCode)
{
case KEY_NUM1:
Sec3ThreadT1 = new Timer();
threeSecT1 = new ThreeSecPressAction(true, this);
Sec3ThreadT1.schedule(threeSecT1, 3000);
break;
case KEY_NUM3:
Sec3ThreadT2 = new Timer();
threeSecT2 = new ThreeSecPressAction(false, this);
Sec3ThreadT2.schedule(threeSecT2, 3000);
break;
}
}
protected void keyReleased(int keyCode)
{
switch (keyCode)
{
case KEY_NUM1:
Sec3ThreadT1.cancel();
threeSecT1.cancel();
if(threeSecDoneT1)
{
//Action 2 Already done by Timer
threeSecDoneT1 = false;
}
else
{
//Generate Action 1
}
break;
case KEY_NUM3:
Sec3ThreadT2.cancel();
threeSecT2.cancel();
if(threeSecDoneT2)
{
//Action 2 Already done by Timer
threeSecDoneT2 = false;
}
else
{
//Generate Action 1
}
break;
}
}
Why is it when I press the button briefly I get Action 1 and Action 2 both. But when I keep the button pressed for 3 seconds I get only Action 2??
Please help me !!!