Giuseppe
Giuseppe

Reputation: 1089

How to open Youtube app main activity

I am trying to open the MainActivity of YouTube app using the code below.

             Intent intent = new Intent(Intent.ACTION_MAIN);
             intent.setPackage("com.google.android.youtube");
             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(intent);

The problem is that the code above work fine when the Youtube app is updated to last version, but fail when the user have an old version of Youtube.

How can I open the YouTube main activity without to care what version the user have?

Upvotes: 1

Views: 2083

Answers (2)

Giuseppe
Giuseppe

Reputation: 1089

I found the solution:

Intent i = app.getPackageManager().getLaunchIntentForPackage("com.google.android.youtube");
if (i != null) 
    app.startActivity(i);

Upvotes: 2

Raphaël Titol
Raphaël Titol

Reputation: 722

You can do a simple intent having ACTION_VIEW :

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("your_youtube_link"));
startActivity(intent)

If your link is a youtube link, the native chooser should ask you what application you want to use : one of your browsers, or the youtube application if available.

Upvotes: 2

Related Questions