Set Duration of the long key listener

Can we set the duration for the Long Key click listener? What I want is if the user continues to touch the screen for 3 seconds, then my long press on the listen button should launch and open a pop-up window for settings.

Thanks in advance.

+1
source share
3 answers

Override the onTouch Listener, then handle the click, the freed event, and set the timer when the button is clicked (event == "pressed")

private Timer timer;

 public LongClickTimer(int seconds) {
            timer = new Timer();
            timer.schedule(new LongClickTask(), seconds *1000);         
        }
 class LongClickTask extends TimerTask {
            public void run() { 
             // 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();
                    }
               return false;
              }
           });
+1
source

3 , 3 ,

0

Android 2.0

public boolean onKeyLongPress(int keyCode, KeyEvent event)

, :

@override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    {
        // do your stuff here
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

, , ...

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

.

0

Source: https://habr.com/ru/post/1570418/


All Articles