views:

220

answers:

1

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?

A: 

You are using a implicit intent, which can match more than one receiver, thus the chooser. You could try switch to an explicit intent model, if you can figure out how to target the youtube activity directly. See developer documentation on explicit vs. implicit intents.

However, it seems the reason for the intent chooser is to let each user decide for themselves which player to use. Is there a good reason you want to bypass this? What if someone has installed a different video player that they prefer?

Edit: To call an explicit intent, you need to know the name of the activity you are trying to start, and you pass additional details as extras i.e.:

Intent intent = new Intent(this, YouTubeViewerActivity.class);
intent.addExtra("URI", Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM"));
startActivity(intent);

However, I totally made up the fact that there is a YouTubeViewerActivity class. As I said, typically if you are asking some outside service, like the YouTube app, to perform an action you use the implicit intent model like you have, so the user has control over what application is used.

Mayra
can you give some sample code thats more understandable? thanks
Praveen Chandrasekaran
may you tell me what is the implicit youtube player Activity? in Logcat, it shows this: "com.google.android.youtube/.PlayerActivity". how can i do ?
Praveen Chandrasekaran
You'd use com.google.android.youtube.PlayerActivity. But as Mayra mentions, this is a bad idea as it will annoy people who maybe have an alternative app installed, and your app will crash if someone runs it on a device without the YouTube application installed. It's the Android equivalent of hardcoding "iexplore.exe" to open a web page.
Christopher