這幾天遇到一個(gè)需求就是app內(nèi)直接打開youtube并帶入搜索的關(guān)鍵字,由于網(wǎng)上沒有搜到解決方案,故解決后記錄一下,也可以給其他人參考。效果如下(直接打開Youtube并搜索VR關(guān)鍵字視頻):

網(wǎng)上能搜到的都是這種寫法:
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
其中id為頻道id,與我的需求不符合,我并不知道頻道id,只曉得目標(biāo)關(guān)鍵字,所以只能繼續(xù)探索。在youtube的網(wǎng)站上直接搜索關(guān)鍵字"VR",瀏覽器的地址為:"https://www.youtube.com/results?search_query=vr",所以我改了方式,直接:
intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://www.youtube.com/results?search_query=vr"));
這種方式呢,在調(diào)用時(shí),系統(tǒng)會(huì)彈出讓用戶選擇應(yīng)用,能被選擇的應(yīng)用有瀏覽器(系統(tǒng)自帶及自己安裝的)、youtube等其他應(yīng)用,我這里需要指定應(yīng)用,故繼續(xù)摸索。在解壓youtube.apk后,查看其主配置文件,會(huì)發(fā)現(xiàn)如下一段:

So,搞定,Youtube確實(shí)有一個(gè)UrlActivity可用來解析加載http,https,vnd.youtube等,所以最后寫法:
intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("https://www.youtube.com/results?search_query=vr")); intent.setClassName("com.google.android.youtube","com.google.android.youtube.UrlActivity"); startActivity(intent);
Over