hammad malik
hammad malik

Reputation: 3

Unable to send sms programatically in android

Hi i am a new android developer.i am trying to send sms through android built-in service SmsManager class my code is running accurately but the message sent through this is not received to other number.My code is as follows

      btnSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String ph=et1.getText().toString();
            String text=et2.getText().toString();
            try{
            SmsManager sms=SmsManager.getDefault();
            sms.sendTextMessage(ph,null, text,null,null);
            Toast.makeText(getApplicationContext(), "sent", Toast.LENGTH_SHORT).show();
            }
            catch(Exception e)
            {
                Toast.makeText(getApplicationContext(), "Message not sent", Toast.LENGTH_SHORT).show();
            }

        }
    });

Upvotes: 0

Views: 1415

Answers (3)

Ashish Kumar
Ashish Kumar

Reputation: 66

  • Please check following permission

    android.permission.SEND_SMS

Upvotes: 1

Kushal Sharma
Kushal Sharma

Reputation: 5973

This works perfect in my application

btnSend.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    sendSMS("YOURNUMBER", Integer.toString(scaleFactor));
    Toast toast = Toast.makeText(getApplicationContext(),"Sending msg...", Toast.LENGTH_SHORT);
    toast.show();
    }
});

private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

and in Manifest :

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

Upvotes: 1

vanste25
vanste25

Reputation: 1774

Do you have a permission? android.permission.SEND_SMS ? You must add it if you want to send messages. Try this code: http://blogs.wrox.com/article/sending-sms-messages-programmatically-in-your-android-application/

Upvotes: 0

Related Questions