Widget that calls speech recognition app

后端 未结 4 1552
一生所求
一生所求 2021-01-04 05:39

I\'m trying to create a widget that contains a single ImageView which, when clicked, starts speech recognition application. I\'ve never worked with widgets and pending inten

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 06:17

    I encounter the same problem, too.
    Sorry that I don't have enough reputation to comment.

    There's no need to use a transparent activity to send a recognition intent.
    Like the answer of zorglub76

    Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
    voiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    voiceIntent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, resultsPendingIntent);
    

    The recognition result will just be in the extra of the resultingPendingIntent
    So all you need to do is:

    In ResultsActivity.onCreate()

    ArrayList voiceResults = this.getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
    

    Be care of the NullPointerException, and you'll get the result from the ArrayList!!

提交回复
热议问题