0

I am trying to create an application where I want to send a message, on click of a button. I have given the permission in Android manifest file.

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

I am using the following code.

package com.example.validateemail;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendSMSActivity extends ActionBarActivity {

    Button btnSendSMS;
    EditText etSMS, etNo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);

        etSMS = (EditText) findViewById(R.id.etSMS);
        etNo = (EditText) findViewById(R.id.etNo);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);

        final String message = etSMS.getText().toString();
        final String number = etNo.getText().toString();


        btnSendSMS.setOnClickListener(new OnClickListener() {


            public void onClick(View v) {

                try { 
                    SmsManager smsManage = SmsManager.getDefault();
                     smsManage.sendTextMessage(number, null, message, null, null);
                     Toast.makeText(getApplicationContext(), "SMS sent.", Toast.LENGTH_LONG).show();



                } catch (Exception e) {
                       Toast.makeText(getApplicationContext(), "SMS failed", Toast.LENGTH_LONG).show();

                }

            }
        });

    }
}

The problem here is I am not able to send the message and it shows the toast "Msg Failed" which I have mentioned in the exception. There is no exception in the Log Cat.

Can anyone tell me what wrong I am doing here ?

I am new to android development. Any help will be appreciated. Thank you.

2
  • Did you run the code? Jan 7, 2015 at 17:36
  • add e.printStackTrace(); to your catch and give the returning stack trace
    – user1178540
    Jan 7, 2015 at 18:34

3 Answers 3

1

You are importing the deprecated SmsManager class. Change your import to android.telephony.SmsManager and use sendTextMessage() from there.

1
  • Thanks @FD_ Yes, I got rid of that depreciated message but the message is not being send. I am editing my original post with entire class.
    – LazyBones
    Jan 7, 2015 at 18:15
0

Follow this tutorial and use android.telephony.SmsManager:

tutorial link Also, did you know that you can send SMS multiple ways?

3
  • thank you @No aNoNym. Can you help me why I am getting an exception ?
    – LazyBones
    Jan 7, 2015 at 18:21
  • the problem here is I don't get an exception in my Log Cat but I get the toast "SMS failed" as display.
    – LazyBones
    Jan 7, 2015 at 18:47
  • display e.getStackTrace() string instead of SMS failed then
    – user1178540
    Jan 7, 2015 at 18:53
0

PlugBunch.com is one of them media platform for entrepreneurs and Individual dedicated to passionately championing and promoting the entrepreneurial ecosystem in the world.We are providing third-party Apache cordova feature plugins Bunch for every one who loves Apache foundation cordova project. http://www.plugbunch.com/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.