Question

I want to start GoogleNow by an Intent and add a search query as an extra (putExtra()) which should be executed in the GoogleNow search engine.

Till now I only used:

Intent intent = new Intent(Intent.ACTION_ASSIST);       
startActivity(intent);

This just causes GoogleNow to open - but how can I add the search string?

Br

Was it helpful?

Solution

So none of this is official, but you can launch Google Now with a query like this:

    final Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
    intent.setPackage("com.google.android.googlequicksearchbox");
    intent.putExtra(SearchManager.QUERY, "Your query");
    startActivity(intent);

OTHER TIPS

Based on the Launcher3 source code I found that the launcher does something similar to my code below to open a Google search:

public static boolean openSearch(Context context) {

        android.app.SearchManager searchManager = (android.app.SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
        if (globalSearchActivity == null) {
            Timber.w("No global search activity found.");
            return false;
        }
        Intent intent = new Intent(android.app.SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setComponent(globalSearchActivity);
        Bundle appSearchData = new Bundle();
        appSearchData.putString("source", getPackageName());

        intent.putExtra(android.app.SearchManager.APP_DATA, appSearchData);
        intent.putExtra(android.app.SearchManager.QUERY, "");
        intent.putExtra(android.app.SearchManager.EXTRA_SELECT_QUERY, true);
        try {
            context.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException ex) {
            Timber.w("Global search activity not found: %s", globalSearchActivity);
            return false;
        }

    }

This has the advantage of opening the recent search history too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top