Here you can find the source of sendSms(@Nonnull Context context, @CheckForNull String number, @Nullable String text)
Parameter | Description |
---|---|
context | The Context to start the intent with |
number | The phone number to send the SMS to |
text | The preset text for the SMS |
public static boolean sendSms(@Nonnull Context context, @CheckForNull String number, @Nullable String text)
//package com.java2s; /*/*from ww w . j a v a2s.c om*/ * Copyright 2013 Luluvise Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; import javax.annotation.Nullable; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.telephony.PhoneNumberUtils; public class Main { public static final String SMS_BODY_EXTRA = "sms_body"; /** * Sends an SMS message to the given number with the passed text. * * FIXME: SMS text is not displayed in Motorola devices * * Note: if the passed {@link Context} is not an activity, the flag * {@link Intent#FLAG_ACTIVITY_NEW_TASK} will automatically be set to avoid * an Android runtime exception. * * @param context * The {@link Context} to start the intent with * @param number * The phone number to send the SMS to * @param text * The preset text for the SMS * @return true if the SMS composer has been opened, false if telephony was * not available on the device or the phone number wasn't in the * accepted format by * {@link PhoneNumberUtils#isWellFormedSmsAddress(String)}. In the * latter case, the SMS composer is shown anyway with no recipients. */ public static boolean sendSms(@Nonnull Context context, @CheckForNull String number, @Nullable String text) { boolean success = true; if (hasTelephony(context)) { if (number == null || !PhoneNumberUtils.isWellFormedSmsAddress(number)) { number = ""; // show empty recipient success = false; } // we can send the sms Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:" + number)); smsIntent.putExtra(SMS_BODY_EXTRA, text); // Intent.EXTRA_TEXT added only as a fallback smsIntent.putExtra(Intent.EXTRA_TEXT, text); if (!(context instanceof Activity)) { smsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } context.startActivity(smsIntent); return true; } else { success = false; } return success; } /** * Checks if the device has the {@link PackageManager#FEATURE_TELEPHONY}. * * @param context * @return true if the telephony is available, false otherwise */ public static boolean hasTelephony(@Nonnull Context context) { PackageManager manager = context.getPackageManager(); return manager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY); } }