how to open youtube app from android application

2019-04-29 18:03发布

i have youtube button which will open a particular *channel* for that i want it to open in youtube *application* in order to access a channel from my application.

Intent intent = new Intent(Intent.ACTION_VIEW , Uri.parse("https://www.youtube.com/channel/UCRmoG8dTnv0B7y9uoocikLw"));
context.startActivity(intent);

But it is opening in browser.

3条回答
smile是对你的礼貌
2楼-- · 2019-04-29 18:14

I found this to be a simple way to do this (assumes you already checked that YouTube is installed):

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.youtube.com/**YOURCHANNEL**"));
intent.setPackage("com.google.android.youtube");
startActivity(intent);
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-29 18:18
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlStr));
startActivity(intent);

urlStr is the url of your desired channel

查看更多
你好瞎i
4楼-- · 2019-04-29 18:21

You should explicitly send it to youtube. You can do this by specifing the package:

intent.setComponent(new ComponentName("com.google.android.youtube","com.google.android.youtube.PlayerActivity"));

Also note that you should also check if youtube is installed!

Intent intent = new Intent(
    Intent.ACTION_VIEW , 
    Uri.parse("https://www.youtube.com/channel/UCRmoG8dTnv0B7y9uoocikLw"));
intent.setComponent(new ComponentName("com.google.android.youtube","com.google.android.youtube.PlayerActivity"));

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
     context.startActivity(intent);
}else{
     //No Application can handle your intent
}
查看更多
登录 后发表回答