How to add voice recognition on my user keyboard

I am developing a special software keyboard that works great. Now I want to add voice recognition (STT) functionality, for which I use the android user api. But the problem is that when I press the microphone button, a voice recognition appears and I get a line corresponding to the voice. But how can I send it again in the onkey event for printing. I have no idea. Please help me. Here is the code:

public void onKey(int pc, int[] key) {
        ic = getCurrentInputConnection();
        al.clear();
        //cv.clear();
        switch (pc) {
        case 45:
                    Speech.i=false;
        Speech.ic=ic;
            Intent intent = new Intent(getApplicationContext(),Speech.class);
            intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            getCurrentInputConnection().commitText(Speech.spoken, 1);
}

and here is the class of speech: -

public class Speech extends Activity {
    static ArrayList<String> results;
    static float[] confidence;
    public static String spoken;
    public static boolean i = true;
    public static InputConnection ic;
    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        // Specify free form input
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
                "or forever hold your peace");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.ENGLISH);
        startActivityForResult(intent, 5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 5 && resultCode == RESULT_OK) {

            results = data
                    .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

            String confidenceExtra = RecognizerIntent.EXTRA_CONFIDENCE_SCORES;
            confidence = data.getFloatArrayExtra(confidenceExtra);
            // TODO Do something with the recognized voice strings
        }

        spoken = null;
        spoken=getWord();
        Main.ic.commitText(spoken, 1);
        //Main m =new Main();
        //m.onKey(28, key)
        //broadcastIntent();
        finish();

    }

    public  String getWord() {
        float max = confidence[0];
        int j = 0;
        for (int i = 0; i < results.size(); i++) {
            if (max < confidence[i]) {
                j = i;
                max = confidence[i];
            }
        }
        //Log.i("main", "" + spoken);
        i = true;
        spoken=results.get(j);
        Log.i("main", "" + spoken);
        return spoken;
    }
/*  public void broadcastIntent()
    {
        Intent intent =new Intent();
        intent.setAction("ttsdone");
        sendBroadcast(intent);
    }*/

}
+3
source share
1 answer

, , , . , SharedPreferences, , onStartInput IME. , , , . , commitText :

 getCurrentInputConnection().commitText("the spoken string here", 1);
0

All Articles