Last active
June 10, 2021 19:34
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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