如果一个ActivityNotFoundException因为显示一个网站而被点击,那么如何捕捉TextView呢?
如果设备没有浏览器,则抛出该异常。
XML:
<TextView
android:id="@+id/tvTextView"
android:autoLink="web" />爪哇:
TextView tvTextView = (TextView) findViewById(R.id.tvTextView);
tvTextView.setText("http://www.stackoverflow.com/");发布于 2014-02-21 15:10:10
您可以使用以下方法检查是否有活动来处理您的意图:
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
}发布于 2014-02-21 15:08:51
将startActivity()包围在try-catch块中。就这样。
您的catch将处理ActivityNotFoundException。
基于2 2Dee回答的更新:
谷歌说,应该做的是,OP必须首先创建一个打开网站的意图,而不是在XML中使用autoLink:web。在onCreate()中,看看是否有Activity来处理它。如果是,请检索TextView并调用setAutoLinkMask(Linkify.WEB_URL)
代码片段:
Intent checkBrowser = new Intent(Intent.ACTION_VIEW);
checkBrowser.setData("http://www.grumpycat.com");
List<ResolveInfo> info = context.getPackageManager().queryIntentActivities(checkBrowser,0);
if(info.getSize() > 0){
TextView tv = (TextView) findElementById(R.id.tv);
tv.setAutoLinkMask(Linkify.WEB_URL);
}发布于 2014-02-21 15:17:43
可以使用此函数检查浏览器是否可用。
public boolean isBrowserAvailable(Context c) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData("http://www.google.com");//or any other "known" url
List<ResolveInfo> ia = c.getPackageManager().queryIntentActivities(i, 0);
return (ia.size() > 0);
}然后,在onCreate中,您将决定是否使其可自动登录。
if (isBrowserAvailable(this)
tvTextView.setAutoLinkMask(Linkify.WEB_URL)https://stackoverflow.com/questions/21937968
复制相似问题