Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecognizerIntent not working; "missing extra calling_package"

I'm having problems using the RecognizerIntent API on Android 2.2. When I call the API using this code:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
startActivityForResult(intent, REQUEST_CODE_VOICE_SEARCH);

which looks like it should work, the search popup says "Unknown problem" on the device and in the logs it states:

01-17 14:25:30.433: ERROR/RecognitionActivity(9118): ACTION_RECOGNIZE_SPEECH intent called incorrectly. Maybe you called startActivity, but you should have called startActivityForResult (or otherwise included a pending intent).
01-17 14:25:30.433: INFO/RecognitionControllerImpl(9118): startRecognition(#Intent;action=android.speech.action.RECOGNIZE_SPEECH;launchFlags=0x800000;component=com.google.android.voicesearch/.IntentApiActivity;B.fullRecognitionResultsRequest=true;S.android.speech.extra.LANGUAGE_MODEL=free_form;end)
01-17 14:25:30.433: INFO/RecognitionControllerImpl(9118): State change: STARTING -> STARTING
01-17 14:25:30.443: ERROR/RecognitionControllerImpl(9118): required extra 'calling_package' missing in voice search intent
01-17 14:25:30.443: ERROR/RecognitionControllerImpl(9118): ERROR_CLIENT
01-17 14:25:30.443: ERROR/RecognitionControllerImpl(9118): ERROR_CLIENT

It looks like the problem is the missing "calling_package" extra; on the RecognizerIntent page it states that this extra is:

The extra key used in an intent to the speech recognizer for voice search. Not generally to be used by developers. The system search dialog uses this, for example, to set a calling package for identification by a voice search API. If this extra is set by anyone but the system process, it should be overridden by the voice search implementation.

As far as I can tell, I don't need to override this extra, so why am I getting this error? How can I fix my code?

like image 446
Isaac Waller Avatar asked Dec 21 '22 19:12

Isaac Waller


2 Answers

I had the exact same problem. I was working on existing code that had android:launchMode="singleInstance" in the activity I was working on. This will not work for speechrecognizer intent. I changed it to android:launchMode="standard". Now let's see how it breaks the rest of my program :)

like image 161
user608383 Avatar answered Mar 23 '23 23:03

user608383


Your original code:

Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
startActivityForResult(intent, REQUEST_CODE_VOICE_SEARCH);

Works correctly. I tested it on my device (HTC Desire), and also compared it to other open-source uses, by executing the following query in Google, and browsing the source code.

RecognizerIntent.ACTION_RECOGNIZE_SPEECH site:code.google.com

One of the output lines in my logcat looks like this:

01-26 13:28:53.268: INFO/RecognitionController(1459): startRecognition(#Intent;action=android.speech.action.RECOGNIZE_SPEECH;component=com.google.android.voicesearch/.IntentApiActivity;B.fullRecognitionResultsRequest=true;S.android.speech.extra.LANGUAGE_MODEL=web_search;S.calling_package=com.test;end)

Run a similar search with one of the built in apps (or downloaded ones), see that it works (and is not a device issue, etc.).

If that works correctly, take the code to a new test project, simply put those lines in the onCreate (Change the result constant to 0), and see if it works.

like image 35
Lior Avatar answered Mar 23 '23 23:03

Lior