2015-10-14 22 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); 
       }