从活动开始 Google 搜索查询 - Android [英] Start Google search query from activity - Android
问题描述
我想知道是否有更简单的方法(或任何方法)来启动带有 Google 搜索查询的浏览器.例如,用户可以选择某个词或词组并单击一个按钮,该活动将使用 Google 搜索查询启动浏览器.
I was wondering if there is an easier way (or any way) to start a Browser with a Google search query. For example user can select a certain word or phrase and click a button and the activity will start the browser with the Google search query.
谢谢.
推荐答案
您可以使用几行代码轻松完成此操作(假设您想在 Google 上搜索fish"):
You can do this quite easily with a few lines of code (assuming you want to search Google for '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);
否则,如果您宁愿启动自己的 Activity 来处理浏览,您应该可以使用 WebView 来实现:http://developer.android.com/reference/android/webkit/WebView.html
Otherwise, if you would rather start up your own Activity to handle the browsing, you should be able to do so with a WebView: http://developer.android.com/reference/android/webkit/WebView.html
我认为这里更好的答案是@zen_of_kermit.不过,如果 Android 允许用户为 ACTION_WEB_SEARCH
提供额外的搜索引擎,而不仅仅是使用 Google,那就太好了.
I think the better answer here is @zen_of_kermit's. It would be nice though, if Android allowed a user to provide the Search engine has an extra though for the ACTION_WEB_SEARCH
, rather than just using Google.
这篇关于从活动开始 Google 搜索查询 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!