您可以使用查询开始两个单独的搜索,如下所示:
优酷搜索
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra(SearchManager.QUERY, "Sunday Bloody Sunday");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
浏览器搜索
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "Sunday Bloody Sunday");
startActivity(intent);
请注意,如果这首歌是众所周知的,浏览器搜索的第一个结果将是 Youtube 视频。
编辑
这是自动为 Youtube 和浏览器创建搜索查询:
private void startSearchIntent(String query) {
List<Intent> browserIntents = new ArrayList<>();
// Find browser intents
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_WEB_SEARCH), 0);
// Create a search intent for each browser that was found
for (ResolveInfo info : resInfo) {
Intent browserIntent = new Intent(Intent.ACTION_WEB_SEARCH);
browserIntent.putExtra(SearchManager.QUERY, query);
browserIntent.setPackage(info.activityInfo.packageName.toLowerCase());
browserIntents.add(browserIntent);
}
// Create Youtube Intent
Intent youtubeIntent = new Intent(Intent.ACTION_SEARCH);
youtubeIntent.setPackage("com.google.android.youtube");
youtubeIntent.putExtra(SearchManager.QUERY, query);
youtubeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(youtubeIntent, query);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, browserIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
然后你只需要调用该方法:
startSearchIntent("Sunday Bloody Sunday");
希望能帮助到你。