9

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.

Rohit Jain
  • 229
  • 1
  • 4
  • 7

4 Answers4

10

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
}
RvdK
  • 19,580
  • 4
  • 64
  • 107
  • Note also, you will probably want to catch an ActivityNotFoundException around your startActivity call to fall back to the browser youtube player. – elimirks Oct 01 '13 at 12:55
  • Correct @elimirks, I've added it to my answer. – RvdK Oct 01 '13 at 12:59
  • 1
    I dont think this is the correct approach, Majorly because the packaname/activity names can be updated/changed by the application provider. All application should be started by correct intent-actions. Though, this might not cause any immediate problem, but can break easily. (Depends on Google...) – Gaurav Arora Oct 01 '13 at 13:14
  • True, but the other solution would be to set Youtube as the main handler of such URLs. – RvdK Oct 01 '13 at 14:32
6

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);
Brian Crider
  • 314
  • 3
  • 13
2
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlStr));
startActivity(intent);

urlStr is the url of your desired channel

johnrao07
  • 6,690
  • 4
  • 32
  • 55
1
Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://www.youtube.com/"));
            intent.setPackage("com.google.android.youtube");
            PackageManager manager = getPackageManager();
            List<ResolveInfo> info = manager.queryIntentActivities(intent, 0);
            if (info.size() > 0) {
                startActivity(intent);
            } else {
                //No Application can handle your intent
            }

This is how u can achieve.. providing only "https://www.youtube.com/" will directly land you into home feed. While one can definitely customize url to redirect to particular channel or video.

Raj Singh
  • 95
  • 6