Launch Preinstalled App from activity (Google Voice Search) Android

巧了我就是萌 提交于 2019-12-06 10:55:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!