Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 意图.行动\网络\搜索:没有后退?_Android_Android Intent_Android Activity - Fatal编程技术网

Android 意图.行动\网络\搜索:没有后退?

Android 意图.行动\网络\搜索:没有后退?,android,android-intent,android-activity,Android,Android Intent,Android Activity,我想开始一个网络搜索活动。与其用谷歌查询启动浏览器,我更愿意使用这种更通用的方法: Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, query); // query contains search string startActivity(intent); 但是,让我大吃一惊的是,后退按钮将我从搜索带到了主屏幕。而不是像ACTION_VIEW那样返回应用程序 基

我想开始一个网络搜索活动。与其用谷歌查询启动浏览器,我更愿意使用这种更通用的方法:

Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, query); // query contains search string
startActivity(intent);
但是,让我大吃一惊的是,后退按钮将我从搜索带到了主屏幕。而不是像ACTION_VIEW那样返回应用程序


基本问题:当使用Intent.ACTION\u WEB\u SEARCH时,如何实现在后台获取当前活动?

我也有同样的问题。我绕了一圈。下面的代码检查您要搜索的文本是否为web URL。如果是URL-此网站将被打开。如果不是URL,它会将其粘贴到谷歌搜索框并显示搜索结果

此代码已针对API 21进行了测试+

private void searchOnGoogle(String query){
        Uri uri;
            if (Patterns.WEB_URL.matcher(query).matches()) // checks if text is ULR
            {
                uri = Uri.parse(query);
            }
            else {
                uri = Uri.parse("https://www.google.pl/search?q="+query);
            }
            Intent intent = new Intent(ACTION_VIEW, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                            Intent.FLAG_ACTIVITY_CLEAR_TASK |
                            Intent.FLAG_ACTIVITY_NEW_TASK);
            try {
                startActivity(intent);
            }
            catch (ActivityNotFoundException e){
                Toast.makeText(getApplicationContext(), getString(R.string.error), Toast.LENGTH_SHORT).show(); // R.string.error - string resource with text you wanna display to user when intent won't run
            }