LazyBones
LazyBones

Reputation: 113

How to send sms through android application?

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.

Upvotes: 0

Views: 366

Answers (3)

Dileep Yadav
Dileep Yadav

Reputation: 1

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/

Upvotes: 0

user1178540
user1178540

Reputation:

Follow this tutorial and use android.telephony.SmsManager:

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

Upvotes: 0

FD_
FD_

Reputation: 12919

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

Upvotes: 1

Related Questions