2017-07-31 14 views
-10

我想知道可以在我的應用程序中搜索youtube或其他應用程序。在另一個應用程序中搜索

String s=" hi "; 

Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=" + s)); 
     startActivity(intent); 

我可以搜索使用的YouTube網站,這個代碼,但我想在Android版YouTube應用(com.google.android.youtube)進行查詢。我將使用下面的代碼進行語音搜索。

if (resultCode == RESULT_OK && null != data) { 

       ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 
       mVoiceInputTv.setText(result.get(0)); 
       String text =mVoiceInputTv.getText().toString(); 

       String filename = text;  // full file name 
       String[] parts = filename.split("\\ "); // String array, each element is text between spaces 
       String beforeFirstSpace = parts[0]; // Text before the first space 

       if(beforeFirstSpace.equalsIgnoreCase("Youtube")) { 
        Intent intent = new Intent(getPackageManager().getLaunchIntentForPackage("com.google.android.youtube")); 
        intent.setAction(Intent.ACTION_SEARCH); 
        intent.putExtra(SearchManager.QUERY,text); 
        startActivity(intent); 

        finish(); System.exit(0); 
} 

使用此代碼我打開了youtube應用程序,但我無法在應用程序中搜索。

+0

除了youtube之外還有什麼? – Kharda

+1

創建一個'Intent Chooser'。 https://developer.android.com/training/basics/intents/sending.html#AppChooser –

+1

用查詢字符串支持'http' URL是非常不尋常的。僅僅因爲YouTube提供這個功能並不意味着其他任何應用都可以。 – CommonsWare

回答

0

你不能輕易做到這一點,因爲其他應用程序可能(也很可能)提供與YouTube不同的另一個API。

例如:

YouTube搜索API: https://www.youtube.com/results?search_query=facebook

Facebook的搜索API: http://www.facebook.com/search/web/direct_search.php?q=%gmail

Gmail搜索API: 的Gmail:http://mail.google.com/mail/?search=query&view=tl&start=0&init=1&fs=1&q=%youtube

你看到有一個不同的模式。因此,您需要手動實施,一個接一個,並根據每個應用提供的搜索API。

+1

謝謝你的回答... –

相關問題