我想通過一個Intent啓動GoogleNow並添加一個搜索查詢作爲額外的(putExtra()),它應該在GoogleNow搜索引擎中執行。如何通過添加搜索查詢的Intent啓動GoogleNow?
到目前爲止我只用:
Intent intent = new Intent(Intent.ACTION_ASSIST);
startActivity(intent);
這只是導致Google即時開 - 但我怎麼可以添加搜索字符串?
溴
我想通過一個Intent啓動GoogleNow並添加一個搜索查詢作爲額外的(putExtra()),它應該在GoogleNow搜索引擎中執行。如何通過添加搜索查詢的Intent啓動GoogleNow?
到目前爲止我只用:
Intent intent = new Intent(Intent.ACTION_ASSIST);
startActivity(intent);
這只是導致Google即時開 - 但我怎麼可以添加搜索字符串?
溴
所以這一切都不是官方的,但你可以用這樣的查詢推出谷歌現在:基於Launcher3源代碼,我發現,發射做類似的東西
final Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.setPackage("com.google.android.googlequicksearchbox");
intent.putExtra(SearchManager.QUERY, "Your query");
startActivity(intent);
我的代碼如下打開谷歌搜索:
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;
}
}
這也有打開最近的搜索歷史的好處。