Skip to content

Instantly share code, notes, and snippets.

@amilcar-sr
Last active June 10, 2021 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amilcar-sr/b9bb71baaa4ce2c770e0616234ad28c2 to your computer and use it in GitHub Desktop.
Save amilcar-sr/b9bb71baaa4ce2c770e0616234ad28c2 to your computer and use it in GitHub Desktop.
Solution to avoid ActivityNotFoundException when attempting to start an activity with ACTION_DIAL. This problem is common in tablets with no call capabilities.
/**
* Method that checks whether the device has apps installed that can handle the Intents or not.
* @param intent Intent that will be checked.
* @return true if there are apps that can handle the Intent, false otherwise.
*/
private boolean checkAppsForIntent(Intent intent) {
PackageManager packageManager = getActivity().getPackageManager();
return intent.resolveActivity(packageManager) != null;
}
//Sample method
private void dialPhoneNumber(String phoneNumber){
final Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
final boolean canMakeCall = checkAppsForIntent(callIntent);
if(canMakeCall){
startActivity(callIntent);
} else {
showWarningDialog();
}
}
//Method that shows a simple AlertDialog
private void showWarningDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setTitle(getString(R.string.no_call_capabilities_title));
dialogBuilder.setMessage(getString(R.string.no_call_capabilities_message));
dialogBuilder.setPositiveButton(android.R.string.ok, null);
dialogBuilder.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment