Skip to content Skip to sidebar Skip to footer

RecognizerIntent.ACTION_RECOGNIZE_SPEECH Blocked When A Tap Occurs

In my main activity I launch a new Intent: Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 'What would you like

Solution 1:

If you're still having your problem, try this:

At the very top, declare a private GestureDetector:

private GestureDetector gestureDetector;

Then, in the onCreate() method, call a method that creates the GestureDetector (there is no reason to do it this way over just creating it in the onCreate() method - it just looks cleaner this way and is more organized):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    gestureDetector = createGestureDetector(this);
}

The createGestureDetector() method looks like this:

private GestureDetector createGestureDetector(Context context) {
    GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
                        //we are creating our gesture detector here
        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {
        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {  //onTap
            return true; //<---this is the key
        }
        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
            return false; //this is the wrong kind of scroll
        }
        @Override
        public void onLongPress(MotionEvent motionEvent) {
        }

        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 
            return false;
        }
    });

    return gestureDetectorTemp;
}

@Override
public boolean onGenericMotionEvent(MotionEvent event) {
    if (gestureDetector != null) {
        return gestureDetector.onTouchEvent(event);
    }
    return false;
}

You have to make sure to include the onGenericMotionEvent() method at the end there. That is what makes sure that your GestureDetector is notified every time a motion event occurs.

The thing that I added is very minute, but very important - by changing the return value to true on the onSingleTapUp() method, you are telling Glass that the event was handled correctly (which in this case, is just doing nothing).

You can read more about GestureDetectors here.


Post a Comment for "RecognizerIntent.ACTION_RECOGNIZE_SPEECH Blocked When A Tap Occurs"