我想通过一个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;
}
}
这也有打开最近的搜索历史的好处。