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

标签 android

如果 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/

相关文章:

android - RetroLamba 不适用于 android studio

java - 通过 Activity 开关传递信息

Android 路由/冒泡事件,因为控件正在消耗 onClick 事件

android - 在 SharedPreferences 中存储数组列表对象

android - GL 表面和可见性 : Gone

Android YouTube API 视频在旋转屏幕或隐藏播放器时暂停

Android:为什么长按也会触发正常点击?

android - 就 Android、XML 或 JSON 的解析性能而言,哪个更好?

android - 与 LiveData 一起使用时返回语句的用途是什么

android - 移动网页 - 在哪里保存设备上的私有(private)数据?