Android Question How to catch ActivityNotFoundException ?

Ardalan

New Member
An "ActivityNotFoundException" occurs because there isn't an app installed on the device.
I don't want to use "Getinstalledpackages" which needs permission. I just want to handle "ActivityNotFoundException". In other words, if the application is installed on phone, startActivty () happens, otherwise shows the message "The application is not installed" instead of ActivityNotFoundException error message.

I have found Java codes. But I don't know how to use them in B4X. I try it but ActivityNotFoundException is not defined.

try {
// here is your code that can potentially throw the exception and the force crash
} catch (ActivityNotFoundException activityNotFound) {
Toast.makeText(this, "your error message", Toast.LENGTH_SHORT).show();
// maybe also log the exception, for future debugging?
}

Thank you for your answers in advance.
 

Ardalan

New Member
B4X:
    Try    
        StartActivity(...)
    Catch (ActivityNotFoundException activityNotFound)
        ToastMessageShow("The app is not installed on your phone!",False)  
    End Try

I tried in this way, but as I said "ActivityNotFoundException" is not defined and I cannot run it without error.
If the Java code catches the exception I would expect the B4X Try ... Catch ... EndTry construct to catch it as well.
 
Upvote 0

Ardalan

New Member
An "ActivityNotFoundException" occurs because there isn't an app installed on the device.
I don't want to use "Getinstalledpackages" which needs permission. I just want to handle "ActivityNotFoundException". In other words, if the application is installed on phone, startActivty () happens, otherwise shows the message "The application is not installed" instead of ActivityNotFoundException error message.

I have found Java codes. But I don't know how to use them in B4X. I try it but ActivityNotFoundException is not defined.

try {
// here is your code that can potentially throw the exception and the force crash
} catch (ActivityNotFoundException activityNotFound) {
Toast.makeText(this, "your error message", Toast.LENGTH_SHORT).show();
// maybe also log the exception, for future debugging?
}

Thank you for your answers in advance.
I could solve it! I hope this solution is also useful for others.

B4X:
Try     
    StartActivity(p.OpenBrowser(...))
            
Catch (p.OpenBrowser(...))
    ToastMessageShow("The app is not installed on your phone!",False)
    
End Try
 
Upvote 0
Top