这是您有兴趣调用的WatchActivity
<activity android:name="WatchActivity" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation" android:allowTaskReparenting="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/v/" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/e/" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="vnd.youtube" />
</intent-filter>
</activity>
这不是默认的activity,就是要直接调用它,你得试试下面的代码
String youtubePackage = "com.google.android.youtube";
ArrayList<IntentFilter> intentFilters = new ArrayList<IntentFilter>();
/*
* Filter to match
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" android:host="www.youtube.com" android:pathPrefix="/watch" />
</intent-filter>
*/
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_VIEW);
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addCategory(Intent.CATEGORY_BROWSABLE);
//filter.addDataScheme("http");
//filter.addDataAuthority("www.youtube.com", null);//port null will allow any port
//filter.addDataPath("/watch", PatternMatcher.PATTERN_PREFIX);
intentFilters.add(filter);
ArrayList<ComponentName> outActivities = new ArrayList<ComponentName>();
getPackageManager().getPreferredActivities(intentFilters, outActivities, youtubePackage);
在最后一次调用之后,outActivities 变量将保存匹配活动(很可能只有一个)。事实上,它不会为我返回任何东西。
要确保 Activitys 与您的 Intent 匹配,您可以查询包
Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.youtube.com/watch?v=EUXnJraKM3k"));
List<ResolveInfo> matchingActivities = getPackageManager().queryIntentActivities(videoIntent, PackageManager.MATCH_DEFAULT_ONLY);
或者放弃所有冗长的程序,然后继续
Intent videoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:<video id>"));
startActivity(videoIntent);
vnd.youtube 是WatchActivity 过滤器接受的方案,如您所见。