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);
I got it! I needed two regular intents wrapped in two pending intents, like this:
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:
My transparrent activity:
To make the activity transparrent, see this post
This is fully functional, and it's based off the ListView widget in the Android SDK. It's not particularly for a widget, but I'm sure you can modify it so that it works for a widget.
Create an activity called SearchActivity:
Add activity to the AndroidManifest.xml
In your custom Widget/View:
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
The recognition result will just be in the extra of the
resultingPendingIntent
So all you need to do is:
In
ResultsActivity.onCreate()
Be care of the
NullPointerException
, and you'll get the result from the ArrayList!!