Android 自动链接为“的TextView上的ActivityNotFoundException”;“网络”;

Android 自动链接为“的TextView上的ActivityNotFoundException”;“网络”;,android,Android,如果由于显示网站而单击了文本视图,如何捕获活动NotFoundException 如果设备没有浏览器,则会抛出该异常 XML: 将try catch块中的startActivity()包围起来。就这些。 您的捕获将处理活动NotFoundException 根据2Dee的答案更新: 应该做的是,OP必须首先创建一个打开网站的意图,比如谷歌,而不是在XML中使用autoLink:web。在onCreate()中,查看是否有活动来处理它。如果是,检索文本视图并调用设置自动链接掩码(Linkify.W

如果由于显示网站而单击了
文本视图
,如何捕获
活动NotFoundException

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

XML:


try catch
块中的
startActivity()
包围起来。就这些。
您的
捕获将处理
活动NotFoundException

根据2Dee的答案更新:
应该做的是,OP必须首先创建一个打开网站的意图,比如谷歌,而不是在XML中使用
autoLink:web
。在
onCreate()
中,查看是否有
活动来处理它。如果是,检索
文本视图
并调用
设置自动链接掩码(Linkify.WEB\u 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);
}
Intent checkBrowser=新意图(Intent.ACTION\u视图);
checkBrowser.setData(“http://www.grumpycat.com");
列表信息=context.getPackageManager().QueryInputActivities(checkBrowser,0);
如果(info.getSize()>0){
TextView tv=(TextView)findElementById(R.id.tv);
设置自动链接掩码(Linkify.WEB_URL);
}

您可以通过以下方式检查是否存在处理您意图的活动:

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
}
Intent Intent=newintent(Intent.ACTION\u视图).setData(Uri.parse()http://www.stackoverflow.com"));
PackageManager=context.getPackageManager();
List infos=manager.querytentActivities(intent,0);
如果(infos.size()>0){
//至少有一个应用程序可以处理您的意图
//将此代码放在onCreate中,从这里仅链接文本视图
//而不是在xml中使用android:autoLink=“web”
Linkify.addLinks(tvTextView、Linkify.WEB_URL);
//或者tvTextView.setAutoLinkMask(Linkify.WEB_URL),就像小孩子建议的那样
}否则{
//没有应用程序可以处理您的意图,如果需要,请通知您的用户
}

您可以使用此功能检查浏览器是否可用

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);


}

不能,它自己处理,我不调用任何东西,我只设置文本。如果OP可以创建
intent
,那么
startActivity()
可以被try-catch包围。)OP说这超出了他的控制范围。:)<代码>链接。必须使用WEB_URL
,请注意:,在
onCreate()
@LittleChild阅读我在你答案下面的评论时,我不是自己调用
startActivity()
。我删除了关于调用startActivity的部分以将其设置正确;)
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
}
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);


}
    if (isBrowserAvailable(this) 

          tvTextView.setAutoLinkMask(Linkify.WEB_URL)