android - 当它要在浏览器中加载 URL 时出现“没有找到处理 Intent 的活动”错误
问题描述
我已经检查了有关No Activity found to handle Intent
错误的类似问题。他们都没有解决我的问题。
当应用程序要使用AppCompatActivity中的 Intent 在 Chrome 浏览器中打开 URL 时,我的 Sentry 日志中出现此错误:
android.content.ActivityNotFoundException: No Activity found to handle Intent
{ act=android.intent.action.VIEW dat=https://www.example.com/...
flg=0x10000000 pkg=com.android.chrome }
这是我的代码:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.android.chrome");
if (MyMethods.isAppInstalled(getApplicationContext(), "com.android.chrome")) {
try {
startActivity(intent);
} catch (ActivityNotFoundException ex) {
SentryLog.warn(ex);
// Chrome browser presumably not installed so allow user to choose instead
intent.setPackage(null);
startActivity(intent);
}
} else {
// Chrome browser presumably not installed so allow user to choose instead
intent.setPackage(null);
startActivity(intent);
}
SentryLog.warn(ex);
已报告错误。
这是isAppInstalled()
MyMethods 类中的方法:
public static boolean isAppInstalled(Context context, String packageName) {
try {
if (context != null) {
context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
}
return true;
} catch (PackageManager.NameNotFoundException e) {
MyLog.w(TAG, new Throwable().getStackTrace()[0].getLineNumber(), e);
e.printStackTrace();
}
return false;
}
有时它会捕捉范围。如您所见,我检查了 chrome 是否安装在设备上,所以如果它不去其他地方,它就会安装!在这种情况下,为什么它无法执行startActivity(intent);
并去捕获范围?
我的代码在一个Activity中,我应该使用intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
还是不使用?
解决方案
无需检查 chrome 它会找到浏览器本身。注意 url 必须以 http 或 https 开头。
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
fragmentActivity.startActivity(browserIntent);
推荐阅读
- database - 如果结果没有在客户端存根处解包,RPC 模型会发生什么?
- android - 在回收站视图中保留编辑文本
- c++ - VS2017:支持 WinXP 的增强二进制文件?
- windows-10 - 在 Windows Anaconda python 上安装 pytorch 时出现“找不到包错误”
- php - 从 .svg 扩展名之外的 Wikipedia API 获取图像
- liferay - Liferay版本从7升级到7.1是否有一键式按钮?
- html - Google Chrome 中的网站图标问题
- java - DES加解密使用2048位java
- python-3.x - Fib Generator answer; Can't pass arguments in a function if it's functioning as an iterable?
- browser - 从浏览器打开电报应用程序