user3013243
user3013243

Reputation: 516

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.

Upvotes: 1

Views: 2413

Answers (1)

Carlos Robles
Carlos Robles

Reputation: 10947

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);

Upvotes: 1

Related Questions