ActivityNotFoundException in TextView with autoLink = "web"

How to catch ActivityNotFoundExceptionif clicked TextViewbecause it displays a website?

If the device does not have a browser, it throws this exception.

XML:

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

Java:

TextView tvTextView = (TextView) findViewById(R.id.tvTextView);
tvTextView.setText("http://www.stackoverflow.com/");
+4
source share
3 answers

You can check if there is an Activity to handle your intent with the following:

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
}
+4
source

Surround the block startActivity()in try-catch. It's all.
Your catchwill handle ActivityNotFoundException.

2Dee:
, , autoLink:web XML, OP -, , Google. 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);
}
+3

You can use this function to check if the browser is accessible.

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


}

and then, in onCreate, you decide whether it will autofill or not.

    if (isBrowserAvailable(this) 

          tvTextView.setAutoLinkMask(Linkify.WEB_URL)
+1
source

Source: https://habr.com/ru/post/1528133/


All Articles