when i click the button i start a activity for youtube video like this:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
if i use this its redirect to the intent chooser to open that video url on browser or youtube app. how to select the default app as youtube programmatically?
my output should open that video directly on youtube player. how? any idea?
Asking for a specific Activity is risky, as YouTube may change their package name which will follow with your application breaking.
Also - there is no guarantee that the YT player is installed on all Android Devices.
To circumvent, here is a code that searches for a youtube activity. If it finds it, it returns an intent to use it directly, otherwise, it keeps a "generic" intent that will result in the system intent chooser to be displayed.
/**
* @param context
* @param url To display, such as http://www.youtube.com/watch?v=t_c6K1AnxAU
* @return an Intent to start the YouTube Viewer. If it is not found, will
* return a generic video-play intent, and system will display a
* chooser to ther user.
*/
public static Intent getYouTubeIntent(Context context, String url) {
Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
final PackageManager pm = context.getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(videoIntent, 0);
for (int i = 0; i < activityList.size(); i++) {
ResolveInfo app = activityList.get(i);
if (app.activityInfo.name.contains("youtube")) {
videoIntent.setClassName(app.activityInfo.packageName, app.activityInfo.name);
return videoIntent;
}
}
return videoIntent;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With