Validate the existence of a URI before launching the intent on Android


Normally I use the try-catch to detect if the process of launching an Intent has been successful or not.

For example to open the equalizer panel of the system configuration, there are models that have it and others the manufacturer has removed it

try {
} catch (ActivityNotFoundException ex) {

With Try-Catch I detect it but I wonder if there is a previous way to know if it is available?

Or the most optimal way, do not spend so many resources ...

asked by Webserveis 18.05.2017 в 18:35

1 answer


Maybe you can use the % method resolveActivityInfo :

boolean activityExiste = intent.resolveActivityInfo(getPackageManager(), 0) != null;

According to the documentation on Android:


If there are no apps on the device that can receive the intent   implicit, your app will fail when you call startActivity() . For   verify first that there is an app to receive the intent, call    resolveActivity() in your Intent object. If the result is not null,   There is at least one app that can manage the intent and it will be safe   call startActivity() . If the result is null, you should not use the   try and, if possible, you should disable the function that invokes it.

answered by 19.05.2017 / 14:01