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 intents, so I'm confused: how to create a pending intent for starting speech recognition activity?
I tried with something like this, but it, of course, fails:
Intent intent = new Intent(); 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); intent.putExtra(RecognizerIntent.EXTRA_RESULTS_PENDINGINTENT, voiceIntent); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main); views.setOnClickPendingIntent(R.id.button, pendingIntent);
Also read =>> Most advanced AI Voice GeneratorsDragon Professional is best as an overall speech recognition software. Dragon Anywhere and Siri are best for iOS users. Cortana is best for Windows users. Google Now is best for Android Mobile devices.
Speechnotes is a simple dictation app that will turn spoken words into text in real time. With no need to make an account, this app is quick and easy to use – just open up the app and press on the microphone icon to get started.
Adding speech-to-text in a Flutter app. In the example app, when a user taps on the mic button, the app starts to listen. As the user begins speaking, the voice is converted into text and displayed on the screen. Users can stop the listening service by clicking on the stop button.
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);
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<String> voiceResults = this.getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
Be care of the NullPointerException
, and you'll get the result from the ArrayList!!
I wanted to create google like widget. I tried zorglub76 solution, but I wasn't able to get voice the result...
I solved it by creating a dummy transparrent activity that handles the voice recognition end-to-end.
It work as follows: Widget->VoiceRecognitionStarterActivity->RecognizerIntent->VoiceRecognitionStarterActivity.onActivityResult.
My widget class:
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent activityIntent = new Intent(context, VoiceRecognitionStarterActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.mic_image, pendingIntent);
activityIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(context.getString(R.string.search_url)));
pendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.search_box_image, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
My transparrent activity:
public class VoiceRecognitionStarterActivity extends Activity
{
private static final String TAG = "VoiceRecognitionStarterActivity";
private int SPEECH_REQUEST_CODE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendRecognizeIntent();
}
private void sendRecognizeIntent()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speak to search");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == SPEECH_REQUEST_CODE)
{
if (resultCode == RESULT_OK) {
Log.d(TAG, "result ok");
Intent searchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.search_url)));
startActivity(searchIntent);
finish();
} else {
Log.d(TAG, "result NOT ok");
finish();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
To make the activity transparrent, see this post
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With