Launch a SMS intent if the device is capable - Android android.telephony

Android examples for android.telephony:SmsManager

Description

Launch a SMS intent if the device is capable

Demo Code

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.util.Log;
import java.util.List;

public class Main{

    private static final String LOG_TAG = "AdapterUtils";
    /** Launch a SMS intent if the device is capable.
     */*from  www.j a v a2  s .  c  o  m*/
     * @param activity The parent activity (for context)
     * @param number The number to sms (not the full URI)
     * @param text The sms body
     */
    public static void launchSmsIntent(final Activity activity,
            String number, String text) {
        // create sms intent
        Uri smsUri = Uri.parse("smsto:" + number);
        Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsUri);
        smsIntent.putExtra("sms_body", text);
        // make sure there is an activity which can handle the intent.
        PackageManager smspackageManager = activity.getPackageManager();
        List<ResolveInfo> smsresolveInfos = smspackageManager
                .queryIntentActivities(smsIntent, 0);
        if (smsresolveInfos.size() > 0) {
            activity.startActivity(smsIntent);
        }
    }

}

Related Tutorials