gpt4 book ai didi

android - 带有自动链接的 TextView 上的 ActivityNotFoundException ="web"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:32 27 4
gpt4 key购买 nike

如果 TextView 因为显示网站而被点击,如何捕获 ActivityNotFoundException

如果设备没有浏览器,则会抛出该异常。

XML:

<TextView
android:id="@+id/tvTextView"
android:autoLink="web" />

Java:

TextView tvTextView = (TextView) findViewById(R.id.tvTextView);
tvTextView.setText("http://www.stackoverflow.com/");

最佳答案

您可以使用以下方法检查是否有一个 Activity 来处理您的 Intent:

Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.stackoverflow.com"));
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
//At least one application can handle your intent
//Put this code in onCreate and only Linkify the TextView from here
//instead of using android:autoLink="web" in xml
Linkify.addLinks(tvTextView, Linkify.WEB_URLS);
// or tvTextView.setAutoLinkMask(Linkify.WEB_URL), as suggested by Little Child
}else{
//No Application can handle your intent, notify your user if needed
}

关于android - 带有自动链接的 TextView 上的 ActivityNotFoundException ="web",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937968/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com