Skip to content Skip to sidebar Skip to footer

Set Duration Of Long Key Press Listener

Can we set duration for Long key press listener? What i want is, if user keeps touching the screen for 3 sec then my long key press listener should trigger and open my pop up for s

Solution 1:

Override onTouch Listener ,then handle pressed,released event and set timer during button pressed (event == "pressed")

private Timer timer;

 publicLongClickTimer(int seconds) {
            timer = new Timer();
            timer.schedule(new LongClickTask(), seconds *1000);         
        }
 classLongClickTaskextendsTimerTask {
            publicvoidrun() { 
             // do what you want            
                timer.cancel(); 
            }
        }
     button.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.v(TAG, "EVENT" + event.toString());
                    if(event.getAction == 2) {// pressed                        new LongClickTimer(5); // schedule for 5 seconds
                    }else{          
                     timer.cancel();
                    }
               returnfalse;
              }
           });

Solution 2:

Inside the Long Press Listener u can set a Handler with 3 Sec Limit and if it reaches 3 sec time then u can run your method in it or else make default method

Solution 3:

From Android 2.0, Activity contains the method

publicbooleanonKeyLongPress(int keyCode, KeyEvent event)

For exemple, a long key press on the back button would be :

@overridepublicbooleanonKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        // do your stuff herereturntrue;
    }
    returnsuper.onKeyLongPress(keyCode, event);
}

Now to open the setting tab you can do following code inside and activity...

Intentintent=newIntent(android.provider.Settings.ACTION_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
activityContext.startActivity(intent);

For detail you can visit for better understanding.

Post a Comment for "Set Duration Of Long Key Press Listener"