2015-10-14 24 views
2

我的问题有点奇怪,但我会尽量具体。 这是我的代码。Google Now截取`Intent.ACTION_VIEW`(奇怪)

if(l.contains("search yahoo")||l.contains("yahoo search")&& !l.contains("search yahoo search")) 
        { 
         String query = l; 
         query = query.replace("search", ""); 
         query = query.replace("yahoo", ""); 
         String url = "http://search.yahoo.com/search?p=" + query; 
         Intent i = new Intent(Intent.ACTION_VIEW); 
         i.setData(Uri.parse(url)); 
         startActivity(i); 
        } 
        if(l.contains("search bing")||l.contains("bing search")&& !l.contains("search bing search")) 
        { 
         String query = l; 
         query = query.replace("search", ""); 
         query = query.replace("bing", ""); 
         String url = "http://www.bing.com/search?q=" + query + "&go=Submit&qs=ds&form=QBLH"; 
         Intent i = new Intent(Intent.ACTION_VIEW); 
         i.setData(Uri.parse(url)); 
         startActivity(i); 
        } 
        else { 
         String v2txt = txt2SpeechWords.get(0); 

         v2txt = v2txt.replace("search", ""); 

         txt2Speech.speak("searching " + v2txt, TextToSpeech.QUEUE_FLUSH, null); 

         Intent search = new Intent(Intent.ACTION_WEB_SEARCH); 
         search.putExtra(SearchManager.QUERY, v2txt); 
         startActivity(search); 
        } 

在我的程序决定是否应该搜索雅虎的部分,或必应Bing,它完美的作品。 但是对于Yahoo!它在网络浏览器中打开,但当我点击设备上的后退按钮时,它会打开Goog​​le Now,除了雅虎&搜索,因为我已经在代码中替换了它们。

为什么会发生这种情况?我很确定问题出现后,它已决定使用谷歌现在使用哪个搜索引擎,它删除了雅虎这个词。为什么Intent.ACTION_VIEW Google Now截获Yahoo!部分虽然这不在Bing部分发生。这是非常讽刺的,因为我几乎复制了雅虎!代码到记事本中,取代Yahoo为Bing &将其粘贴到Android Studio中。

+0

你有没有编码的查询URL编码(例如,“我的大狗”将是“我+大+狗”。)?我相信有一个Java类(URLEncode?)来做到这一点。它可能无法解决你原来的问题,但它肯定会解决未来的问题。 – kkirigaya

+1

@kkirigaya我会尽力而为,让你知道。同样感谢回复 –

+1

@kkirigaya也是这两个部分唯一的主要区别是'雅虎'被替换为bing,但是我知道'if'声明有效。第二个是2'url'是不同的。所以也许如果我在雅虎部分添加一个'+“”'会起作用。我不确定。不过,我一定会尝试你的建议和我的想法,并尽快告诉你。谢谢。 –

回答

0

我犯了最愚蠢的错误。在雅虎部分,它是if,并且它也是if。 Bing的if声明必须为else if。另外我的问题有一个错误。在谷歌现在它不会取代雅虎!因此if声明是错误的。我只是注意到了。对不起。

正确的代码:

if(l.contains("search yahoo")||l.contains("yahoo search")&& !l.contains("search yahoo search")) 
       { 
        String query = l; 
        query = query.replace("search", ""); 
        query = query.replace("yahoo", ""); 
        String url = "http://search.yahoo.com/search?p=" + query; 
        Intent i = new Intent(Intent.ACTION_VIEW); 
        i.setData(Uri.parse(url)); 
        startActivity(i); 
       } 
       else if(l.contains("search bing")||l.contains("bing search")&& !l.contains("search bing search")) 
       { 
        String query = l; 
        query = query.replace("search", ""); 
        query = query.replace("bing", ""); 
        String url = "http://www.bing.com/search?q=" + query + "&go=Submit&qs=ds&form=QBLH"; 
        Intent i = new Intent(Intent.ACTION_VIEW); 
        i.setData(Uri.parse(url)); 
        startActivity(i); 
       } 
       else { 
        String v2txt = txt2SpeechWords.get(0); 

        v2txt = v2txt.replace("search", ""); 

        txt2Speech.speak("searching " + v2txt, TextToSpeech.QUEUE_FLUSH, null); 

        Intent search = new Intent(Intent.ACTION_WEB_SEARCH); 
        search.putExtra(SearchManager.QUERY, v2txt); 
        startActivity(search); 
       }