Vibrate after a long click - android

I am developing an Android app and at some point want to give the user ability to long press a view and after a short vibrate do some action.
I have tried onLongClick but it is triggered even on a very very short touch. I have read that the wait time cannot be changed; which makes it useless in my case. Am I missing something about that event?
If no, onTouchEvent is the only solution I guess. Is there a way to capture a long press with that event?

It sounds like you have not enabled long clicks. Please check whether calling the following method helps (taken from Android View Reference):
public void setLongClickable (boolean longClickable)

Related

How do I prevent auto click cheats in an android app?

I built an android app where the user uses a stopwatch to try to get it to stop on exactly 1 second. There is also a second gamemode where the user tries to start and stop the stopwatch as fast as possible and get the lowest time. There are several auto clicker apps that you can install that will start and stop the stopwatch exactly 1 second apart and also double click the screen within milliseconds.
My question is what is the best way to prevent cheating by this method? Are there libraries designed for this? Thanks for any help!
This question has been answered here.
Basically, use an API to check for clicks using accessibility services, but you can't straight out block artificial clicks because people who use accessibility features need them. Instead you can measure the time between clicks or the frequency of clicks and block the ones that are 'too perfect'.
E.g. if someone gets exactly 1 second between clicks 10 times in a row, they're probably cheating. If someone clicks however many times per second at exactly the same timing of 0.X ms between clicks, they're definitely cheating.
I was focusing on tap or touch event so that I can differentiate between human behaviour and other than human. Here's what I have done:
#Override
public boolean dispatchTouchEvent (MotionEvent event) {
if(event.getToolType(0) == MotionEvent.TOOL_TYPE_UNKNOWN) {
return false;
} else {
return super.dispatchTouchEvent(event);
}
}

Accessibility Event Issue

I want to make my own accessibility service using the API's, but when
some buttons are clicked, no event of type VIEW_CLICKED is logged.
In accessibility.xml,
I set android:accessibilityFlags="flagDefault|flagIncludeNotImportantViews
I'm not sure Why when I click the Accept and Continue button in Chrome (as part of the first run activity), no click event is logged.
Any help would be greatly appreciated.
You need to specify android:accessibilityEventTypes. To intercept clicks you need to use typeViewClicked option.
Something like that:
android:accessibilityEventTypes="typeViewClicked"
For more information you can check Android documentation.

Application Close Using Combination of Button

I have a requirement where I need to close my opened application using combination of keys.
Like when user will open my application and user will press twice combination of Power button and Option then application will close. Anyone can suggest me how it is possible or not?
Thanks .
You can setOnTouchListener() on each of the buttons that must be combined and use some boolean variables to control which buttons are pressed at a given time, then finish() your application when appropriate. See Touch Release method in Android for an example (which needs a little fix BTW: onTouch() must return true when the event has been consumed, false otherwise).
For the Power button you can override your activity's onKeyDown() and onKeyUp() methods. There is a key code for the Power button at http://developer.android.com/reference/android/view/KeyEvent.html

Android: how to combine Activity.onTouchEvent() and View.setOnLongClickListener() capabilities

In Android, I'm trying to capture user touches as well as User long-touches. I have an Activity and in it I override the onTouchEvent() method to handle a variety of screen touches.
I'm trying to incorporate "Long Presses" into my repertoire of User Interface choices.
I can't find a Activity.onLongTouchEvent() for me to override.
My application also has a SurfaceView and I see that I can do this:
sv.setOnLongClickListener (new View.OnLongClickListener()
{
#Override
public boolean onLongClick (View v)
{
SendAToast();
return false;
}
});
When I implement that code, it works exactly like it should.
However, now my onTouchEvent() code is never called even when I don't touch the screen long enough for it to be a "Long Press".
Is anybody aware of a way to get these two bits of code to work together?
Edit:
After I posted this, a co-worker showed me the "OnGestureListener" interface. Here's an example:
http://www.ceveni.com/2009/08/android-gestures-detection-sample-code.html
I use the interface to capture long presses, and it even provides the screen coordinates to work with (which the OnLongClickListener does not). So, it seems to do the trick.
Why this function not automatically part of the Activity? It sure seems like core functionality to me.
I would move the on touch stuff into the view's onTouchEvent instead of the Activity.

Send a long click (or any touch event) to a View in ActivityInstrumentationTestCase2

When doing unit testing in Android activities using ActivityInstrumentationTestCase you could use TouchUtils to "send" a long click to a view in the activity with longClickView (ActivityInstrumentationTestCase test, View v)
ActivityInstrumentationTestCase was deprecated in favour of ActivityInstrumentationTestCase2.
So the longClickView is no longer available; it is deprecated and recommends using ActivityInstrumentationTestCase2 "which provides more options for configuring the Activity under test"
I cannot find how to do this operation (sending a touch event to a view) in ActivityInstrumentationTestCase2.
Can anyone show me an example of how to do this now?
EDIT
Further addition to the question. Is there anyway I can get the test to wait on a textview being updated. I have a button which fetches something from the network and displays it in a TextView. I'd like to test pressing that button and then examining the resulting text.
EDIT 2
I'm sorry for continuing to add to this question but they are related. So...when I press that button I expect the vibrator to vibrate. Is there an accepted was to confirm that this has occurred?
I just found this in SO.
Is this the recommended way to go? To call the performLongClick() and performClick() methods on the buttons directly?

Categories

Resources