Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open youtube app from android application

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.

like image 882
Rohit Jain Avatar asked Oct 01 '13 12:10

Rohit Jain


3 Answers

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.

like image 145
Raj Singh Avatar answered Nov 08 '22 23:11

Raj Singh


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
}
like image 20
RvdK Avatar answered Nov 08 '22 22:11

RvdK


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);
like image 34
Brian Crider Avatar answered Nov 08 '22 21:11

Brian Crider