打开全局搜索作为叠加层

时间:2016-07-05 21:55:53

标签: android android-launcher android-searchmanager

我正在编写一个启动器,希望能够在Google App中以叠加方式而不是全屏方式打开搜索。

到目前为止,我只找到了一种在Google App全屏中打开搜索的方法,如下所示(取自AOSP Launcher3源代码):

 public static boolean openSearch(Context context) {

        SearchManager searchManager = (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", "launcher-search");

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

    }

我知道这是可能的,因为像Nova和Action Launcher这样的其他发射器设法做到了......

1 个答案:

答案 0 :(得分:5)

想出来......

public static boolean showGlobalSearchOverlay(Context context) {
    ComponentName globalSearchActivity =
            new ComponentName("com.google.android.googlequicksearchbox",
                    "com.google.android.apps.gsa.queryentry.QueryEntryActivity");

    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(globalSearchActivity);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    try {
        context.startActivity(intent);
        return true;
    } catch (Throwable e) {
        Timber.w("Unable to show search overlay");
        return false;
    }
}