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

Android examples for Phone:SMS

Description

Launch a SMS intent if the device is capable.

Demo Code

/*//from w w w .  j  av a  2s. c o m
 * Copyright 2015 OpenMarket 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.
 */
//package com.java2s;
import android.app.Activity;

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.
     *
     * @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) {
        Log.i(LOG_TAG, "Launch SMS intent to " + number);
        // 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