Widget that calls speech recognition app

后端 未结 4 1541
一生所求
一生所求 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 05:59

    I got it! I needed two regular intents wrapped in two pending intents, like this:

    // this intent points to activity that should handle results
    Intent activityIntent = new Intent(context, ResultsActivity.class);
    // this intent wraps results activity intent
    PendingIntent resultsPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
    
    // this intent calls the speech recognition
    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);
    
    // this intent wraps voice recognition intent
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, voiceIntent, 0);
    rv.setOnClickPendingIntent(R.id.btn, pendingIntent);
    

提交回复
热议问题