Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide the Intent Chooser window in android?

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?

like image 372
Praveen Avatar asked Feb 26 '23 23:02

Praveen


1 Answers

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;
}
like image 133
Guy Avatar answered Mar 07 '23 18:03

Guy