Android YouTube app Play Video Intent

前端 未结 18 1400
太阳男子
太阳男子 2020-11-22 11:17

I have created a app where you can download YouTube videos for android. Now, I want it so that if you play a video in the YouTube native app you can download it too. To do t

18条回答
  •  借酒劲吻你
    2020-11-22 11:49

    EDIT: The below implementation proved to have problems on at least some HTC devices (they crashed). For that reason I don't use setclassname and stick with the action chooser menu. I strongly discourage using my old implementation.

    Following is the old implementation:

    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(youtubelink));
    if(Utility.isAppInstalled("com.google.android.youtube", getActivity())) {
        intent.setClassName("com.google.android.youtube", "com.google.android.youtube.WatchActivity");
    }
    startActivity(intent);
    

    Where Utility is my own personal utility class with following methode:

    public static boolean isAppInstalled(String uri, Context context) {
        PackageManager pm = context.getPackageManager();
        boolean installed = false;
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }
    

    First I check if youtube is installed, if it is installed, I tell android which package I prefer to open my intent.

提交回复
热议问题