问题
I am trying to use the following code to launch Google Voice Search from my app. It works fine on the Nexus One where Google Voice Search is a downloaded App, however it does not work on my Galaxy Nexus where it comes preinstalled. when it gets to the getLaunchIntentForPackage, the result is NULL. Can anyone help out?
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.voicesearch");
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
回答1:
I ran into the exact same problem. It seems to be caused by the way google has packaged up the voice component in ICS. It's not a standalone app anymore. It's integrated into the search bar through the general search app.
The fix is really easy:
Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
startActivity(intent);
I found this through this tutorial that's more about general speech recognition: http://www.jameselsey.co.uk/blogs/techblog/android-how-to-implement-voice-recognition-a-nice-easy-tutorial/
There's some code in there to verify that the voice recognizer is installed:
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() == 0)
{
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
来源:https://stackoverflow.com/questions/9074313/launch-preinstalled-app-from-activity-google-voice-search-android