要求特定的活动是有风险的,因为YouTube可能会更改其包名,随着您的申请被中断。
另外 - 无法保证YT播放器安装在所有Android设备上。
为了规避,这里是一个搜索YouTube活动的代码。如果它找到它,它会直接返回一个意图,否则它会保留一个“通用”的意图,这会导致系统意图选择器被显示。
/**
* @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;
}
来源
2011-07-13 05:56:35
Guy
你可以给一些示例代码更容易理解吗?感谢 – Praveen 2010-05-21 17:35:04
你可否告诉我什么是隐式YouTube玩家活动?在Logcat中,它显示了这个:“com.google.android.youtube/.PlayerActivity”。我能怎么做 ? – Praveen 2010-05-21 18:11:49
您将使用com.google.android.youtube.PlayerActivity。但是,正如Mayra所提到的,这是一个坏主意,因为它会惹恼那些可能安装了替代应用程序的人,并且如果有人在未安装YouTube应用程序的情况下在设备上运行应用程序,您的应用程序就会崩溃。这是Android的等价硬编码“iexplore.exe”来打开一个网页。 – 2010-05-21 18:48:54