Code example for ExtractedTextRequest

        // Unintentional empty constructor for singleton. 
    private static int getCursorPosition(InputConnection connection) {
        if (null == connection) return INVALID_CURSOR_POSITION;
        final ExtractedText extracted = connection.getExtractedText(new ExtractedTextRequest(), 0);
        if (extracted == null) {
            return INVALID_CURSOR_POSITION;
        return extracted.startOffset + extracted.selectionStart;
     * @param connection connection to the current text field. 
     * @param separators characters which may separate words 
     * @return the word that surrounds the cursor, including up to one trailing 
     *   separator. For example, if the field contains "he|llo world", where | 
     *   represents the cursor, then "hello " will be returned. 
    public static String getWordAtCursor(InputConnection connection, String separators) {
        // getWordRangeAtCursor returns null if the connection is null