Reputation: 37577
Which is the best and more optimized strategy to check if the screen has been touched for 2 seconds or more without stopping the main UI thread?
I have checked some sample codes but I'm not sure which is the best approach to achieve it, and also I need to do it without stopping the main UI thread.
Thanks
Upvotes: 0
Views: 2212
Reputation: 685
I used a OnTouchListener
on a RelativeLayout
. I hope this will help you.
RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
ll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
if (clickDuration >= MIN_CLICK_DURATION) {
Toast.makeText(MainActivity.this, "TOUCHED FOR" + clickDuration + "MS", Toast.LENGTH_SHORT).show();
}
longClickActive = false;
break;
case MotionEvent.ACTION_DOWN:
if (longClickActive == false) {
longClickActive = true;
Toast.makeText(MainActivity.this, "touch!", Toast.LENGTH_SHORT).show();
startClickTime = Calendar.getInstance().getTimeInMillis();
}
break;
case MotionEvent.ACTION_MOVE:
if (longClickActive == true) {
longClickActive = false;
}
break;
}
return true;
}
});
Upvotes: 1
Reputation: 49807
You can implement an OnTouchListener
like this:
public abstract class TouchTimer implements View.OnTouchListener {
private long touchStart = 0l;
private long touchEnd = 0l;
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
this.touchStart = System.currentTimeMillis();
return true;
case MotionEvent.ACTION_UP:
this.touchEnd = System.currentTimeMillis();
long touchTime = this.touchEnd - this.touchStart;
onTouchEnded(touchTime);
return true;
case MotionEvent.ACTION_MOVE:
return true;
default:
return false;
}
}
protected abstract void onTouchEnded(long touchTimeInMillis);
}
You would use it like this:
view.setOnTouchListener(new TouchTimer() {
@Override
protected void onTouchEnded(long touchTimeInMillis) {
// touchTimeInMillis contains the time the touch lasted in milliseconds
}
});
The method onTouchEnded()
is called once the touch ends.
Upvotes: 6
Reputation: 16537
All gesture detection parts of Android code internally use combination of Handler class and postDelayed method. You can use it to post pieces of code to be run after arbitrary amount of time (for example 2 seconds).
wait 2 secs for code to be run
Runnable runnable;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
runnable = new Runnable() {
@Override
public void run() {
//do something
}
};
getHandler().postDelayed(runnable, 2000);
} else if (ev.getAction() == MotionEvent.ACTION_UP
|| ev.getAction() == MotionEvent.ACTION_CANCEL) {
getHandler().removeCallbacks(runnable);
}
return super.dispatchTouchEvent(ev);
}
see: http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)
Upvotes: 0