YosiFZ
YosiFZ

Reputation: 7900

Android Send SMS Crash

I want to sent SMS with my Application with :

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
smsIntent.setData(Uri.parse("smsto:" + sms));
smsIntent.putExtra("smsto", sms);
smsIntent.putExtra("sms_body", "MYSMSBOBY");
mActivity.startActivity(smsIntent);

It's work fine in devices that there is SMS Application, but in some devices i get this crash error:

No Activity found to handle Intent { act=android.intent.action.VIEW dat=smsto:xxxxxxxxxx flg=0x10000000 (has extras) }

Any idea how i can recognize if SMS Application installed on the device?

Upvotes: 0

Views: 577

Answers (2)

Anthone
Anthone

Reputation: 2290

<!-- SMS -->
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>


    /**
     * Test if device can send SMS
     * @param context
     * @return
     */
    public static boolean canSendSMS(Context context) {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY);
    }

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

Any idea how i can recognize if SMS Application installed on the device?

While you can use PackageManager to see if there's any app to handle your intent, that's should not be really of your concern at all. What you should take care of is just the crash itself, so instead of just:

mActivity.startActivity(smsIntent);

you should at least have generic exception handling code:

try {
  mActivity.startActivity(smsIntent);
} catch ( Exception e ) {
    e.printStackTrace();
    // show toast or something so user knows why it is not working
}

and catch any failure of startActivity(). You may also want to make create separate catch for this particular type of exception, ActivityNotFoundException

Upvotes: 1

Related Questions