0

I am making a simple android application to send message to a mobile network. I am using the following code but when I enter the Phone number and the Text Message and Click the Send Button it does not sends the Message and says Message Sending Failed.

My Main Activity "Main.java" is this..

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.telephony.SmsManager;
import android.widget.Toast;

public class Main extends ActionBarActivity {

    Button Btn;
    EditText textphoneNo;
    EditText Message;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);

        Btn = (Button) findViewById(R.id.SendSMS);
        textphoneNo = (EditText) findViewById(R.id.TextPhoneNo);
        Message = (EditText) findViewById(R.id.TextSMS);


        Btn.setOnClickListener(new View.OnClickListener() {
           public void onClick(View view) {
              sendSMSMessage();
           }

        protected void sendSMSMessage() {
            Log.i("Send SMS", "");

            String phoneNo = textphoneNo.getText().toString();
            String message = Message.getText().toString();

            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, message , null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent.",
                Toast.LENGTH_LONG).show();
             } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                "SMS Sending Failed.",
                Toast.LENGTH_LONG).show();
                e.printStackTrace();
             }
          }
          @SuppressWarnings("unused")
        public boolean onCreateOptionsMenu(Menu menu) {
             // Inflate the menu; this adds items to the action bar if it is present.
             getMenuInflater().inflate(R.menu.main, menu);
             return true;
        }
    });

    }
  }

When the send button is clicked the control goes to the catch statement and message is not sent, please tell me that what is the problem here which is not letting the control to go to the try statement.

Sikandar Ejaz
  • 53
  • 3
  • 9
  • 1
    Do you have any texts available to send? If so, what does your stacktrace report? – Wolfish Jul 22 '14 at 11:31
  • 2
    Thanks guys my problem is solved, actually I was not using the permission in the manifest file.. now I have used the permission and the sms is sent. Thanks. @Wolfish please be careful with your words you don't how how deep they are gonna hurt the listener. – Sikandar Ejaz Jul 22 '14 at 11:44
  • possible duplicate of [Send SMS in android](http://stackoverflow.com/questions/4967448/send-sms-in-android) – Maveňツ Jul 22 '14 at 11:46

3 Answers3

2

You must add this permission in your AndroidManifest.xml

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

Did you?

  • 1
    Thanks a lot @Jose Montero Problem Solved. Actually I was not using the permission in manifest file now I have entered the permission and the problem is solved. Thanks again. – Sikandar Ejaz Jul 22 '14 at 11:45
0

Use Log.i("phonenumber",textphoneNo.getText().toString()) to see phoneNumber Value. I think it's value is null.

0

Make sure that u added sms send permission in the Manifest.xml file

prakash
  • 109
  • 12