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