我想知道是否有更简单的方法(或任何方式)启动带有Google搜索查询的浏览器.例如,用户可以选择某个单词或短语并单击按钮,该活动将使用Google搜索查询启动浏览器.
谢谢.
zen*_*mit 55
Intent类定义了一个专门用于Web搜索的操作:
http://developer.android.com/reference/android/content/Intent.html#ACTION_WEB_SEARCH
以下是如何使用它的示例:
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, query); // query contains search string
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
nic*_*ild 26
您可以使用几行代码轻松完成此操作(假设您要在Google上搜索"fish"):
String escapedQuery = URLEncoder.encode(query, "UTF-8");
Uri uri = Uri.parse("http://www.google.com/#q=" + escapedQuery);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
否则,如果您希望启动自己的Activity来处理浏览,您应该可以使用WebView执行此操作:http://developer.android.com/reference/android/webkit/WebView.html
我认为这里更好的答案是@ zen_of_kermit's.但是,如果Android允许用户提供搜索引擎有一个额外的ACTION_WEB_SEARCH,而不是仅仅使用谷歌,那将是很好的.
在#给我的麻烦:
Uri uri = Uri.parse("https://www.google.com/search?q="+query);
Intent gSearchIntent = new Intent(Intent.ACTION_VIEW, uri);
activity.startActivity(gSearchIntent);
Run Code Online (Sandbox Code Playgroud)
小智 5
我最近试过这个。这似乎工作正常。如果要进行任何修改,请告诉我,因为我是 android 开发的新手。
mEdit = (EditText)findViewById(R.id.editText);
Run Code Online (Sandbox Code Playgroud)
在您的点击视图中,
String q = mEdit.getText().toString();
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH );
intent.putExtra(SearchManager.QUERY, q);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)