在我的用户界面中,我有一个文本视图来显示列表内容。我为该文本视图启用了自动链接和应用选择器对话框。在youtube链接作为内容的情况下(比如 https:/youtu.bex。),在从应用选择器对话框中选择youtube应用时,youtube应用打开后出现错误画面(错误代码400).我不知道为什么会这样。我提供了下面的代码。
public static void getAppChooserDialog(Context context, String url){
List<Intent> targetedShareIntents = new ArrayList<>();
List<String> sharePackages = new ArrayList<>();
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(createViewIntent(url),0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = createViewIntent(url);
if (!info.activityInfo.packageName.equalsIgnoreCase(context.getPackageName()) &&
!sharePackages.contains(info.activityInfo.packageName)) {
if (targetedShare != null) {
targetedShare.setPackage(info.activityInfo.packageName);
sharePackages.add(info.activityInfo.packageName);
}
targetedShareIntents.add(targetedShare);
}
}
if(!targetedShareIntents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
context.getString(R.string.complete_action_using));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[]{}));
context.startActivity(chooserIntent);
}
}
private Intent createViewIntent(String url){
Intent shareIntent = new Intent(Intent.ACTION_VIEW);
shareIntent.setData(Uri.parse(url));
return shareIntent;
}
YouTube应用不支持普通网址的意图(不知道为什么)。对于一个网址 https://youtu.be/videoId
你需要分享 vnd:youtube:videoId