简体   繁体   中英

Android Intent To Open Application Not Working

I am trying to open the Google Voice Search Application

    Intent i;
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage("com.google.android.voicesearch");
        if (i == null)
            throw new PackageManager.NameNotFoundException();
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
    }

This does not launch the voice search application, however if I use com.google.android.apps.maps as the package name then the Google Maps app is opened.

I don't understand why Voice Search is not opening, even though the package name is correct.

Solution

Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
startActivity(intent);

Please see Launch Preinstalled App from activity (Google Voice Search) Android for more information on the solution.

Thank you.

As you can read here , getLaunchIntentForPackage (String packageName)

Return a "good" intent to launch a front-door activity in a package [...]

The current implementation will look first for a main activity in the category CATEGORY_INFO, next for a main activity in the category CATEGORY_LAUNCHER, or return null if neither are found.

so, in the intent, the category will already be set. If you manually change it, probably you are breaking the intent, since the category could not be the correct one that the manager found.

so, just remove the line

i.addCategory(Intent.CATEGORY_LAUNCHER);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM