Under what condition could the code sample below crash your application? How would you modify the code to avoid this potential problem? Explain your answer.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type
startActivity(sendIntent);
Explicit intents specify the component to start by name (the fully-qualified class name).
Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. If there is more than one application registered that can handle this request, the user will be prompted to select which one to use. However, if there is no App can handle this intent, in this case, your application will crash when you invoke startActivity().
To avoid this, should first verify that there is at least one application registered in the system that can handle the intent:
// Verify that there are applications registered to handle this intent
// (resolveActivity returns null if none are registered)
+ if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivity(sendIntent);
+ }