10 November 2014

How Send SMS in android Pragmatically

This is very simple.So There are two type to send SMS.First one is SMS manager API and second one is Build-in sms application.In here i have only explain how to send SMS via SMS manager API.
This is the class which support to send SMS.when we using SMS manager API we can't see any UI level change when sending message.

import android.content.Context;

import android.telephony.SmsManager;

import android.widget.Toast;

public class SendSms {

    Context context;

    public SendSms(Context context){

      this.context=context;

    }

    String phoneNo="telnumber(+9471 1111111)";

    public void SendSms(String sms){

        try {

            SmsManager smsManager = SmsManager.getDefault();

            smsManager.sendTextMessage(phoneNo, null, sms, null, null);

            Toast.makeText(context, "SMS Sent!",

                    Toast.LENGTH_LONG).show();

        } catch (Exception e) {

            Toast.makeText(context,

                    "SMS faild, please try again later!",

                    Toast.LENGTH_LONG).show();

            e.printStackTrace();

        }

    }

}

And make sure you have add SMS send uses-permission.
 <uses-permission android:name="android.permission.SEND_SMS" />

And happy coding..

No comments:

Post a Comment