我的Android应用程序中有一个帮助菜单,可以显示帮助网页或帮助视频.随着时间的推移,我们遇到了一些奇怪的问题,意图返回为空,因为他们没有浏览器或类似的东西(至少这是我之前基于Firebase异常报告的假设).因此,我们让代码变得更复杂,以便通过几种不同的方式来判断YouTube视频URL的任何应用程序.如果系统说没有应用程序可以处理它,请try 以不同的方式构建意图.
它终于发生在我身上了,所以这很好!然而,我的手机上安装了Chrome和YouTube应用程序,但ResolveActivity()返回的结果为空.
在下面的代码中,youtubeuri = "https://www.youtube.com/watch?v=JHb9wYcmQcg"
和videourlstring = "https://www.youtube.com/watch?v=JHb9wYcmQcg"
(在这个特殊情况下,URL最终是相同的).
但是,即使我同时安装了Chrome和YouTube,它也找不到一个活动来启动对YouTube URL的基本ACTION_VIEW
的意图.
最新版本的安卓操作系统有什么变化吗?如果没有特殊许可,我的操作方式就不再起作用了吗?我做错了什么?
private void startVideo(HelpTutorial tutorial) {
String videoid = tutorial.getVideoID();
Uri youtubeuri = Uri.parse("https://www.youtube.com/watch?v=" + videoid);
Intent intent = new Intent(Intent.ACTION_VIEW, youtubeuri);
List<ResolveInfo> list = this.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() == 0) {
String videourlstring = tutorial.getVideoURLString();
intent = new Intent(Intent.ACTION_VIEW, Uri.parse(videourlstring));
}
if (intent.resolveActivity(getPackageManager()) != null) {
this.startActivity(intent);
} else {
AlertDialogFragment alert = new AlertDialogFragment();
alert.setTitle(getString(R.string.no_browser_installed_title));
alert.setMessage(getString(R.string.no_browser_installed_message));
alert.show(getSupportFragmentManager(), "No Browser Error");
}
}
编辑:
如果我只返回startActivity()
,即使resolveActivity()
返回空值,那么YouTube应用程序实际上是打开的.所以这个应用程序已经安装好了,并且可以正常工作了.但出于某种原因,我的应用程序不允许看到这一点.