7

In my Android application, I use the following code to start the messaging app and fill in a default text for a text message:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"+USERS_PHONE_NUMBER));
intent.putExtra("sms_body", "DUMMY TEXT");
startActivity(intent);

This works in most cases. But unfortunately, on some devices I get the following error message:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=sms:+XXXXXXXXXX (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1510)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
at android.app.Activity.startActivityForResult(Activity.java:3131)
at android.app.Activity.startActivity(Activity.java:3237)

Obviously, the intent that I created cannot be handled.

  • Is there any mistake in my SMS intent code?
  • How can I prevent the application from crashing if the intent cannot be handled?

Should I use PackageManager.queryIntentActivities() or is there another way of solving this problem?

Thanks in advance!

caw
  • 30,999
  • 61
  • 181
  • 291

3 Answers3

14

I haven't tried this intent specifically, but the simplest way will probably be to add try and catch block

try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Display some sort of error message here.
}

Since you can't count on the specific Android device to have the Messaging app (some tablets for example don't have telephony services), you have to be ready.

It is a good practice in general when you're starting external activities, to avoid crashes in your app.

Mark Ursino
  • 31,209
  • 11
  • 51
  • 83
Talihawk
  • 1,299
  • 9
  • 18
  • Thank you very much! I can't say if it works because the error occurred that rarely. But as it is an exception "try-catch" should do the job, right? And is there any difference between "Intent.ACTION_VIEW" and "Intent.ACTION_SENDTO" in my case? – caw Feb 29 '12 at 23:53
  • We're using the try-catch method in our app and it does work. Personally I've used ACTION_SENDTO before and it works. I guess in most cases it will act just the same (works the same on my phone here), but I'm not sure. If you want to send a message, it's probably better to use ACTION_SENDTO – Talihawk Mar 01 '12 at 00:11
2

Here is the code that will open the SMS activity pre-populated with the phone number to which the SMS has to be sent. This works fine on emulator as well as the device.

Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber); 
JaydeepW
  • 3,237
  • 1
  • 25
  • 27
1

Here is a method I use to safely open activities on Android, and give the user some feedback if the activity is not found.

public static void safeOpenActivityIntent(Context context, Intent activityIntent) {

        // Verify that the intent will resolve to an activity
          if (activityIntent.resolveActivity(context.getPackageManager()) != null) {
              context.startActivity(activityIntent);
          } else {
              Toast.makeText(context, "app not available", Toast.LENGTH_LONG).show();
          }
}

(I think I got it from one of the Google Developers videos on youtube, but now I can't find the video...)

TouchBoarder
  • 6,422
  • 2
  • 52
  • 60